Solved

SQL Query To Return Running Subtotal - Year to Date

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

863 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

23 Experts available now in Live!

Get 1:1 Help Now