我有一张这样的 table :
SELECT value FROM 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
这适用于 oracle ;) 但它也应该适用于 sqlite
关于SQLite:SELECT 语句中的累加器(求和)列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3785995/