Sample table ID: (num is a key so there wouldn't be any duplicates)
num
1
5
6
8
2
3
Desired output:
(Should be sorted and have a cumulative sum column)
num cumulative
1 1
2 3
3 6
5 11
6 17
8 25
This is one solution I got:
select a.num, sum(b.num) from ID a, ID b where b.num <= a.num group by a.num order by a.num;
You can use a temporary variable to calculate the cumulative sum:
SELECT a.num,
(@s := @s + a.num) AS cumulative
FROM ID a, (SELECT @s := 0) dm
ORDER BY a.num;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With