oracle 下一行,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)

)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值