4

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;
4
  • AFAIK, MySQL can't really do this sort of thing. This being referencing previous rows. You could use a temporary table, I suppose, but it might be better doing this sort of thing clientside. Commented Oct 2, 2011 at 21:12
  • Just a fun problem i got while playing with mysql. Trying to use joins. Commented Oct 2, 2011 at 21:15
  • You could certainly do it with variables, common someone will certainly show the @sum answer. I'm lazy. Commented Oct 2, 2011 at 21:47
  • I didn't want to do it with variables. Just sql query. Commented Oct 2, 2011 at 21:52

4 Answers 4

7

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;
Sign up to request clarification or add additional context in comments.

1 Comment

Anyone using the new MySql 8.0 should instead use CTE
3

I think I figured out the solution.

Select num as n, 
       (select sum(num) from ID where num <= n)
from ID order by n;

2 Comments

Actually, it doesn't work if you have duplicates, i.e. 1,2,3,3,4,5, you'll get 0,1,3,3,9,13, which I don't think is what you want.
Yup, that totally makes sense. I was looking at only unique values. Will update that in the question.
1

Since MySQL 8, cumulative sums are ideally calculated using window functions. In your case, run:

SELECT num, SUM(num) OVER (ORDER BY num) cumulative
FROM id

2 Comments

do u know why w/o order by it ouputs all sum not cumulative?
0

as these answer i already tested in my project and actually i want to know which one is faster so i also posted this here which one is faster

declare @tmp table(ind int identity(1,1),col1 int)
insert into @tmp
select 2
union
select 4
union
select 7
union 

select 5
union
select 8
union 
select 10


 SELECT t1.col1,sum( t2.col1)
    FROM @tmp AS t1 LEFT JOIN @tmp t2 ON t1.ind>=t2.ind
    group by t1.ind,t1.col1

select t1.col1,(select sum(col1) from  @tmp as t2 where t2.ind<=t1.ind)
from @tmp as t1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.