SQL Cumulative Sum on Rows

I this temporary table in SQL:

DUEMONTH      DUEYEAR      LATE      ACHIEVED      UNSURE      OUTSTANDING      TOTAL      MONTHNAME
1      2010      0      0      42      0      42      January 2010
2      2010      2      4      37      0      43      February 2010
3      2010      0      2      43      0      45      March 2010
4      2010      0      1      36      0      37      April 2010
5      2010      1      5      32      0      38      May 2010
6      2010      2      2      19      0      23      June 2010
7      2010      0      3      35      0      38      July 2010
8      2010      0      1      32      0      33      August 2010
9      2010      1      2      32      0      35      September 2010
10      2010      0      1      32      0      33      October 2010
11      2010      2      0      40      0      42      November 2010
12      2010      1      0      39      0      40      December 2010
1      2011      0      5      52      0      57      January 2011
2      2011      0      0      32      0      32      February 2011
3      2011      0      2      41      0      43      March 2011
4      2011      0      0      7      0      7      April 2011

I want to be able to calculate the cumulative sum on the ACHIEVED and TOTAL columns in 2 new columns.
So one column called ACHIEVED_CUMULATIVE will be:
0,4,6,7,12,13,15,16,18,19,19,19,24,24,26,26

How can i do this as some samples that ive seen require some sort of identity column which i dont really have.

Here is the code that i have to retrieve the first bit of data
SELECT MONTH(DUE_DATE) AS DUEMONTH, 
YEAR(DUE_DATE) AS DUEYEAR, 
SUM(CASE WHEN LATE = 1 THEN 1 ELSE 0 END) AS LATE, 
SUM(CASE WHEN ACHIEVED = 1 THEN 1 ELSE 0 END) AS ACHIEVED,  
SUM(CASE WHEN DODGY = 1 THEN 1 ELSE 0 END) AS DODGY, 
SUM(CASE WHEN OUTSTANDING = 1 THEN 1 ELSE 0 END) AS OUTSTANDING, 
SUM(CASE WHEN LATE = 1 THEN 1 ELSE 0 END) + SUM(CASE WHEN ACHIEVED = 1 THEN 1 ELSE 0 END) + SUM(CASE WHEN DODGY = 1 THEN 1 ELSE 0 END) + SUM(CASE WHEN OUTSTANDING = 1 THEN 1 ELSE 0 END) AS TOTAL,
DATENAME(m,DATEADD(mm,MONTH(DUE_DATE),-1)) + ' ' + CONVERT(NVARCHAR,YEAR(DUE_DATE)) as MONTHNAME
FROM #COUNTNEXT 
GROUP BY YEAR(DUE_DATE), MONTH(DUE_DATE) ORDER BY DUEYEAR, DUEMONTH

Open in new window

LVL 1
AVONFRSAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AkenathonCommented:
What's your DBMS brand and version?
0
AVONFRSAuthor Commented:
SQL Server 2008 R2
0
SharathData EngineerCommented:
try this.
;WITH CTE as (
SELECT MONTH(DUE_DATE) AS DUEMONTH, 
YEAR(DUE_DATE) AS DUEYEAR, 
SUM(CASE WHEN LATE = 1 THEN 1 ELSE 0 END) AS LATE, 
SUM(CASE WHEN ACHIEVED = 1 THEN 1 ELSE 0 END) AS ACHIEVED,  
SUM(CASE WHEN DODGY = 1 THEN 1 ELSE 0 END) AS DODGY, 
SUM(CASE WHEN OUTSTANDING = 1 THEN 1 ELSE 0 END) AS OUTSTANDING, 
SUM(CASE WHEN LATE = 1 THEN 1 ELSE 0 END) + SUM(CASE WHEN ACHIEVED = 1 THEN 1 ELSE 0 END) + SUM(CASE WHEN DODGY = 1 THEN 1 ELSE 0 END) + SUM(CASE WHEN OUTSTANDING = 1 THEN 1 ELSE 0 END) AS TOTAL,
DATENAME(m,DATEADD(mm,MONTH(DUE_DATE),-1)) + ' ' + CONVERT(NVARCHAR,YEAR(DUE_DATE)) as MONTHNAME
FROM #COUNTNEXT 
GROUP BY YEAR(DUE_DATE), MONTH(DUE_DATE) ),
CTE2 as (SELECT *,ROW_NUMBER() over (ORDER BY DUEYEAR,DUEMONTH) rn FROM CTE )
SELECT *,(SELECT SUM(ACHIEVED) FROM CTE2 C2 WHERE C2.rn <= C1.rn) ACHIEVED_CUMULATIVE 
  FROM CTE2 C1 
 ORDER BY DUEYEAR, DUEMONT

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
AVONFRSAuthor Commented:
That is perfect. Worked great thanks!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.