日期:2014-05-17 浏览次数:21188 次
create table ta(id number, val number);
create table tb(id number, val number);
--给插入测试数据
declare
begin
for i in 1..210 loop
insert into ta values(i,i+10);
end loop;
end;
--把表ta数据按照你的要求复制到表tb,先得到总过需要循环多少遍,然后没循环一次,查询出对应的100条数据,然后union统计的那一行数据,再将这101条数据插入到表tb中去。
declare
pages number;
begin
select ceil(count(*)/100) into pages from ta;
for i in 1..pages loop
insert into tb
select * from ta where rownum between (i-1)*100+1 and i*100
union all
select null id, (select sum(val) from ta where rownum between (i-1)*100+1 and i*100) val from dual;
end loop;
end;
------解决方案--------------------
这要根据真实业务弄。
一个简单的例子
insert into b (
select col1 ,sum(col1)
from (select col1,ceil(rownum/99) rn ,rownum from a01)
group by rollup(rn ,col1));
即,按需求把数据分组,然后每99行算一个标识符,根据标识符分组求和汇总