日期:2014-05-17 浏览次数:20916 次
create table table1(id1 int, num1 int); insert into table1(id1,num1) values(1,20); insert into table1(id1,num1) values(2,30); insert into table1(id1,num1) values(3,50); insert into table1(id1,num1) values(4,10); insert into table1(id1,num1) values(5,20); update table1 t1 set t1.num2=(select sum(num1) from table1 t2 where t2.id1<=t1.id1 );
------解决方案--------------------
这个可以使用窗口函数来实现。
测试数据:
CREATE TABLE T33 ( ID NUMBER(4), NUM1 NUMBER(4) ); INSERT INTO T33 VALUES(1, 20); INSERT INTO T33 VALUES(2, 30); INSERT INTO T33 VALUES(3, 50); INSERT INTO T33 VALUES(4, 10); INSERT INTO T33 VALUES(5, 20);
------解决方案--------------------
SELECT t.id1, t.num1, SUM(t.num1) OVER(ORDER BY t.id1) num2 FROM table1 t