SQLite:SELECT 语句中的累加器(求和)列

标签 sql sqlite sum cumulative-sum accumulator

我有一张这样的 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/

相关文章:

mysql - 带有 .new 的 Rails 控制台出错

r - 从 n 中选择 k 个不同且递增的索引

algorithm - 6SUM : Given a set S of n integers, 是否存在 S 的子集恰好有 6 个元素之和为 0?如何比 O(n^3) 做得更好?

sql - T-SQL 选择第一个实例

php - 在 MySQL 数据库中存储 MS SQL Server 凭据

java - 对一段时间内的每一天做一个查询,把它变成一个查询

php - 将月份名称添加到每月总计中

SQL:重用函数返回的值?

sqlite - MvvmCross - 如何在 Windows 存储后台任务中访问 SQLite?

sql - 基于概念模式编写 sql 脚本