Solved

SQL Query To Return Running Subtotal - Year to Date

Posted on 2014-03-04
5
472 Views
Last Modified: 2014-03-05
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
0
Comment
Question by:larisa1970
  • 2
  • 2
5 Comments
 
LVL 48

Expert Comment

by:PortletPaul
ID: 39905421
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)
0
 

Author Comment

by:larisa1970
ID: 39905424
2008
0
 
LVL 16

Accepted Solution

by:
DcpKing earned 500 total points
ID: 39905442
As you have your date embedded in the table as an integer, try this (assuming that your table is called Revenue - you didn't say):

select r.id, r.Y_M, r.R, (	select sum(R) 
					from dbo.Revenue 
					where	Y_M <= r.Y_M 
						and	Y_M > ((r.Y_M / 100)*100)	
				)	
from dbo.Revenue r
where ID = 1
group by id, Y_M, R

Open in new window


hth

Mike
0
 

Author Comment

by:larisa1970
ID: 39906371
thanks a lot, this is perfect
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 39907028
"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
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question