Mysql cumulative sum

Create sample table with data:
CREATE TABLE SimpleTable
(
  Id INT
 ,SumValue INT
);
 
INSERT INTO SimpleTable
VALUES 
(1,100),(2,150),(3,200)
,(4,250),(5,75),(6,500);

SELECT * FROM SimpleTable
Output:

Cumulative SUM column using MySQL Variable:
set @csum := 0;
select id, SumValue, (@csum := @csum + SumValue) as Cumulative_sum
from SimpleTable
order by id;
Output:

Post a Comment

0 Comments