Click here to Skip to main content
15,884,986 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to achieve this output, I have a columns like ID,Date,Qty but i want cumulative qty.

ID           Date       Qty       Cummulative
1          2011-02-23   100        100
1          2011-03-18   300        400
1          2013-04-23   100        500
2          2010-01-20    50         50
2          2013-03-23   100        150


How to achieve this , i'm searching in google for this but that's not satisfied my requirement..

can anybody help me out of this...
Posted

Hi Naveen ,

Please look into the example.

It may be help you .

SQL
Create table #tt1 (sno int identity,Amt int)

insert into #tt1 values (100)
insert into #tt1 values (200)
insert into #tt1 values (300)
insert into #tt1 values (400)

select * from #tt1

select a.sno,a.Amt,Sum(b.Amt) as Cum_Amt from #tt1 a cross join #tt1 b
where a.sno>=b.sno group by a.sno,a.Amt
order by a.sno,a.Amt
 
Share this answer
 
Comments
Naveen.Sanagasetti 26-Feb-13 0:47am    
I tried like this but my output is mismatch to my requirement

select a.Proj_code,a.Material_Id,a.DED_Description,a.DED_Short_Desc,a.Planned_date,

a.Planned_Material_qty, sum(b.Planned_Material_qty) As Total

from #TempTable a cross join #TempTable b where (b.Material_Id=a.Material_Id)

group by a.Proj_code,a.Material_Id,a.DED_Description,a.DED_Short_Desc,a.Planned_date,

a.Planned_Material_qty order by Material_Id
Try this->

SQL
declare  @t table
(
    id int,
    SomeNumt int
)

insert into @t
select 1,10
union
select 2,12
union
select 3,3
union
select 4,15
union
select 5,23


select t1.id, t1.SomeNumt, SUM(t2.SomeNumt) as sum
from @t t1
inner join @t t2 on t1.id >= t2.id
group by t1.id, t1.SomeNumt
order by t1.id
 
Share this answer
 
SQL
SELECT      T1.SL,
            T1.GroupName,
            T1.Amount,
            SUM(T2.Amount) as CumulativeSum
FROM  @Temp T1 INNER JOIN
            @Temp T2 on T1.SL >= T2.SL
GROUP BY T1.SL,T1.GroupName, T1.Amount
ORDER BY T1.SL


For more in details:

http://cybarlab.blogspot.com/2013/06/cumulative-sum-in-sql.html[^]
 
Share this answer
 
Hi,

Try like this....
SQL
SELECT ID,Date,Qty (SELECT SUM(Qty) FROM tablname WHERE ROWNUMBER() OVER(ORDER BY ID) <= T.RowNo) 'CumulativeSum'
FROM (SELECT ROW_NUMBER() OVER(ORDER BY ID) 'RowNo', ID,Date,Qty FROM table_Name)T

Regards,
GVPrabu
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900