Solved

SQL Query To Return Running Subtotal - Year to Date

Posted on 2014-03-04
5
464 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

758 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now