如何对额外列中的行元素求和并找出某些字段大于 x 的数据?

How to sum row elements in extra column and find out data where some of field is > than x?

SELECT *FROM 'product'
row_id quantity
1 100
2 200
3 300
4 400

我想要 select x 个产品,数量总和大于 100。

这是我的预期输出:

row_id quantity total
2 200 300
3 300 600
4 400 1000

使用SUM()window函数:

SELECT *
FROM (
  SELECT *, SUM(quantity) OVER (ORDER BY rowid) total
  FROM product
)
WHERE total > 100

不支持 window 函数的 SQLite 的敌人版本使用相关的子查询:

SELECT *
FROM (
  SELECT p.*, (SELECT SUM(pp.quantity) FROM product pp WHERE pp.rowid <= p.rowid) total
  FROM product p
)
WHERE total > 100

参见demo