Cumulative Function in SQL Server.

Arul44ece
Posted by Arul44ece under Sql Server category on | Points: 40 | Views : 1382
Cumulative Sum:

The Cumulative sum is achieved in table using inner join or Cross join with in the table.
Prevoius column value is added with current value is Cumulative Sum.

The below is the simple example for Cumulative Sum.



Create Table dts
(

dat Date,

qty Int
)



Insert Into dts Values(
'2012-10-06' ,150),
('2012-10-07', 265),
('2012-10-12', 895),
('2012-10-15 ',4155),
('2012-10-16' ,650),
('2012-10-20', 23)


-- Using Cross Join

Select A.dat as [Created Date],SUM(B.qty) Quantity

From dts A

Cross Join dts B

where A.dat >=B.dat Group By A.dat


-- Using Inner Join for both Table


Select A.dat as [Created Date],SUM(B.qty) Quantity

From dts A

inner Join dts B

on A.dat >=B.dat Group By A.dat

Comments or Responses

Login to post response