添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

有表 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)
)