[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL Query To Return Running Subtotal - Year to Date

Posted on 2014-03-04
5
Medium Priority
?
482 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 49

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 2000 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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

650 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