我有一张这样的 table :


value
1
3
13
1
5

我想添加一个累加器列,以便得到以下结果:
value  accumulated
1      1
3      4
13     17
1      18
5      23

我怎样才能做到这一点?我想做什么的真实名字是什么?谢谢

最佳答案

尝试这种方式:

select value,
(select sum(t2.value) from table t2 where t2.id <= t1.id ) as accumulated
from table t1

但是如果它不适用于您的数据库,只需按某种顺序添加订单
select value,
(select sum(t2.value) from table t2 where t2.id <= t1.id order by id ) as accumulated
from table t1
order by id

这适用于甲骨文;),但也应该适用于sqlite

09-10 03:44
查看更多