Link to home
Start Free TrialLog in
Avatar of larisa1970
larisa1970

asked on

SQL Query To Return Running Subtotal - Year to Date

Hello,

Data -
ID  Y_M       Revenue
1   201201  60
1   201202  50
1   201203  60
...
1   201212  10
1   201301  60
1   201302  20
1   201303  40
1   201304  10
...
2   201201  60
...

Result:
Data -
ID  Y_M       R  YTD
1   201201  60   60
1   201202  50  110
1   201203  60  170
...
1   201212  10  480
1   201301  60   60
1   201302  20   80

Is it posible to have a query to return Year-to-Date number for each row
(meaning if its March - the YTD is a sum of Jan-Feb-March for that year)?
Ultimately i would need a YTD/Previous Year YTD - but I don't think that it's possible in a single query, or is it?

Thank you
Avatar of PortletPaul
PortletPaul
Flag of Australia image

it is very important for this answer to know what version of SQL Server you are using.

It is possible to produce running totals, the method used differs (with older versions it is harder and less efficient).

please specify the version(s)
Avatar of larisa1970
larisa1970

ASKER

2008
ASKER CERTIFIED SOLUTION
Avatar of DcpKing
DcpKing
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
thanks a lot, this is perfect
"thanks a lot, this is perfect"

Just my humble best    <grin>

I would imagine that you'll want to put it into a procedure and call the procedure with the year number and the ID number ...

hth

Mike