Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 cumulative sum

Author  Topic 

mkgmahi
Starting Member

24 Posts

Posted - 2011-09-06 : 03:41:39
I need to find the cumulative sum of qty for the a particular month. How can i do it.

The problem is The qty comes from different dates

I tried the below sql but not working

SELECT
a.DayCount
, a.Sales
, SUM(b.Sales) AS RunningTotal
FROM
Sales a
JOIN Sales b ON b.DayCount <= a.DayCount
GROUP BY
a.DayCount
, a.Sales
ORDER BY
a.DayCount
, a.Sales

mkgmahi
Starting Member

24 Posts

Posted - 2011-09-06 : 03:45:05
I need the Report the below way. My parameter is from and todate should be given for a particular month

Date Qty totalqty
02/07/2011 20 20
04/07/2011 10 30
05/07/2011 -10 20


Could you please help me out for the solution
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2011-09-06 : 04:49:22
something like

select t.date,t.qty,t1.cumtotal
from table t
cross apply (select sum(qty) as cumtotal
from table
where date<=t.date) t1


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -