oracle sql 怎么实现 下一行的数值等于上一行总和加这一行的值

有表 t1, 字段 n1 varchar(20), n2 numeric
表内数据
a 20.5
b 30

希望得到以下这样的结果(第三列的值 = 本行字段n2的值 + 前一行第三列的值)

a 20.5 20.5
b 30 50.5

sql怎么写?

select a.rn, a.id, a.value + case when ((select sum(value) from aaa a2 where  rownum <= a.rn -1  ) <> 0) or ((select sum(value) from aaa a2 where  rownum <= a.rn -1  ) is not null) then (select sum(value) from aaa a2 where  rownum <= a.rn -1  )
else 0 end
from
(
  select rownum rn, id, value  from aaa 
)a

数据

insert into aaa (ID, VALUE, ROWID)
values ('a', 10.000000000, 'AABVdjAABAAAjWyAAA');

insert into aaa (ID, VALUE, ROWID)
values ('b', 15.000000000, 'AABVdjAABAAAjWyAAB');

insert into aaa (ID, VALUE, ROWID)
values ('c', 20.000000000, 'AABVdjAABAAAjWyAAC');

insert into aaa (ID, VALUE, ROWID)
values ('d', 25.000000000, 'AABVdjAABAAAjWyAAD');

CREATE TABLE "AAA"
   ( "ID" VARCHAR2(36) NOT NULL ENABLE,
 "VALUE" NUMBER(18,9)
   )

原文地址:https://www.cnblogs.com/zhangzhifeng/p/2207483.html