Sql cumulative sum

Sql cumulative sum

Example:

CREATE TABLE Test (id int,Number int);

insert into test
select 1,10
union
select 2,154
union
select 3,33
union
select 4,15
union
select 5,230

SELECT * FROM Test
I have a table like given below:


Answers
The following query returns the cumulative balance As Number Column.
select t1.id, t1.Number, SUM(t2.Number) as CumulativeValue
from test t1
inner join test t2 on t1.id >= t2.id
group by t1.id, t1.Number
order by t1.id
Result:

Post a Comment

0 Comments