比如,表ttt有四列 a b c d,要相加B C D A B c d BB 5 1 6 BB 12 2 14 BB 8 3 25 BB 9 4 34select a,b,c,d,B+C+D 求和 from ttt; 显示 A B c d 求和 BB 5 1 6 12 BB 12 2 14 28 BB 8 3 25 35 BB 9 4 34 47
可能我没说清楚,我的表如下 A B 求和 BB 5 5 BB 12 17 BB 8 25 BB 9 34 就是说是把B列中的值累加来得到求求和列的值,关键是第一行和第二行怎么加?
create talbe temp_table( aa varchar2(10), bb number, 和 number);create or replace procedure ccc tempa number; tempb number; tempccc; begin declare cursor c1 is select a,b from tt; begin open c1; truncate table temp_table; loop fetch c1 into tempa,tempb; exit when c1%notfound; begin tempccc=tempccc+tempb; insert into temp_table (a,b,和) values(tempa,tempb,tempccc); commit; end loop; close c1; end; end; select * from temp_table
select a,b,sum(b) over(order by rownum) from table_name
A B c d
BB 5 1 6
BB 12 2 14
BB 8 3 25
BB 9 4 34select a,b,c,d,B+C+D 求和 from ttt;
显示
A B c d 求和
BB 5 1 6 12
BB 12 2 14 28
BB 8 3 25 35
BB 9 4 34 47
A B 求和
BB 5 5
BB 12 17
BB 8 25
BB 9 34
就是说是把B列中的值累加来得到求求和列的值,关键是第一行和第二行怎么加?
create talbe temp_table(
aa varchar2(10),
bb number,
和 number);create or replace procedure ccc
tempa number;
tempb number;
tempccc;
begin
declare cursor c1 is select a,b from tt;
begin
open c1;
truncate table temp_table;
loop
fetch c1 into tempa,tempb;
exit when c1%notfound;
begin
tempccc=tempccc+tempb;
insert into temp_table (a,b,和) values(tempa,tempb,tempccc);
commit;
end loop;
close c1;
end;
end;
select * from temp_table