您好我想计算名为“percent_sum”的列的累计总和,并希望将其存储在累计列(70)中,我的表名是localbanya_1,这是我的表结构
sku_id new_total percent_sum
1 12 30
2 8 20
3 9 20
4 16 10
5 10 40
这就是我所期待的
sku_id new_total percent_sum cumulative(70)
1 12 30 30
2 8 20 30+20
3 9 20 30+20+20
4 16 10 30+20+20+10
5 10 40 30+20+20+10
亲切的帮助。
提前谢谢。
答案 0 :(得分:4)
您可以使用变量:
SELECT sku_id, new_total, percent_sum,
@s := @s + percent_sum AS cumulative
FROM mytable
CROSS JOIN (SELECT @s := 0) AS var
ORDER BY sku_id
答案 1 :(得分:1)
您可以使用相关查询对此进行求和:
SELECT t.sku_id,t.new_total,t.percent_sum,
(select sum(s.percent_sum) from YourTable s
where s.sku_id <= t.sku_id) as cumulative
FROM YourTable t