Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to add up the monthly total?


How to add up the monthly total?

Author
Message
Seattlemsp
Seattlemsp
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 64
Here is example table and data

CREATE TABLE [dbo].[sales](
[date_value] [datetime]NOT NULL,
[monthly_total] [int] NOT NULL
)

insert into sales ( date_value, monthly_total)
values
('2013-01-01', 100),
('2013-02-01', 90),
('2013-03-01', 120),
('2013-04-01', 200),
('2013-05-01', 200),
('2013-06-01', 300),
('2013-07-01', 1500),
('2013-08-01', 600),
('2013-09-01', 200),
('2013-10-01', 800),
('2013-11-01', 500),
('2013-12-01', 50),
('2012-01-01', 110),
('2012-02-01', 130),
('2012-03-01', 30),
('2012-04-01', 60),
('2012-05-01', 10),
('2012-06-01', 30),
('2012-07-01', 20),
('2012-08-01', 150),
('2012-09-01', 170),
('2012-10-01', 30),
('2012-11-01', 100),
('2012-12-01', 80)

How can I create a query to get below result?

Year Month MonthlySale Accumulated Total
2012 Jan 110 110
2012 Feb 130 240
2012 Mar 30 270
2012 Apr 60 330
2012 May 10 340
2012 Jun 30 370
2012 Jul 20 390
2012 Aug 150 540
2012 Sep 170 710
2012 Oct 30 740
2012 Nov 100 840
2012 Dec 80 920
2013 Jan 100 1020
2013 Feb 90 1110
2013 Mar 120 1230
2013 Apr 200 1430
2013 May 200 1630
2013 Jun 300 1930
2013 Jul 1500 3430
2013 Aug 600 4030
2013 Sep 200 4230
2013 Oct 800 5030
2013 Nov 500 5530
2013 Dec 50 5580


Thanks!
SQLCJ
SQLCJ
Mr or Mrs. 500
Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)

Group: General Forum Members
Points: 539 Visits: 576

select date_value,monthly_total,(select sum(monthly_total) from sales s where date_value <= tst.date_value )
monthly_total_sum from
(
select *,ROW_NUMBER() over ( order by date_value )tst from sales
)tst order by date_value


MyDoggieJessie
MyDoggieJessie
SSCarpal Tunnel
SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)SSCarpal Tunnel (4.9K reputation)

Group: General Forum Members
Points: 4905 Visits: 7365
To add to SQLCJ's post, you will need to include the ORDER BY date_value at the end to get the OP's expected result - and some ugly string manipulation for the date

SELECT CONVERT(CHAR(4), GETDATE(), 120) + ' ' + CONVERT(CHAR(4), GETDATE(), 100) ,
monthly_total ,
( SELECT SUM(monthly_total)
FROM #sales s
WHERE date_value <= tst.date_value
) AS monthly_total
FROM ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY date_value ) tst
FROM #sales
) tst
ORDER BY date_value A



______________________________________________________________________________
"Never argue with an idiot; They'll drag you down to their level and beat you with experience" ;-)
SQLCJ
SQLCJ
Mr or Mrs. 500
Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)Mr or Mrs. 500 (539 reputation)

Group: General Forum Members
Points: 539 Visits: 576
I was about to update the post and saw your comment Thank you MyDoggieJessie for pointing that out.
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45104 Visits: 39912
MyDoggieJessie (3/7/2014)
To add to SQLCJ's post, you will need to include the ORDER BY date_value at the end to get the OP's expected result - and some ugly string manipulation for the date

SELECT CONVERT(CHAR(4), GETDATE(), 120) + ' ' + CONVERT(CHAR(4), GETDATE(), 100) ,
monthly_total ,
( SELECT SUM(monthly_total)
FROM #sales s
WHERE date_value <= tst.date_value
) AS monthly_total
FROM ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY date_value ) tst
FROM #sales
) tst
ORDER BY date_value A



Ah, be carefull now. That contains an (X2+X)/2+X Triangular join. In this case, it generates 324 internal rows for the 24 original rows. While it "runs fast" for such a small quantity of rows, these things can be server killers. With just 10,000 original rows, SQL Server will generate 50,015,000 internal rows and all of the logical reads to go with it. A cursor and WHILE loop or a Recursive CTE would actually run much faster.

Please see the following article about "Triangular Joins" for more information.
http://www.sqlservercentral.com/articles/T-SQL/61539/

OR... if you follow the rules, there's a bit of undocumented code known at the "Quirky Update". If you don't follow the rules, it can bite ya. It will, however, do a million row running or grouped running total in just seconds and is faster than even the new methods in SQL Server 2012. Plese see the following article for that.
http://www.sqlservercentral.com/articles/T-SQL/68467/

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
dwain.c
dwain.c
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4255 Visits: 6431
Jeff Moden (3/7/2014)
MyDoggieJessie (3/7/2014)
To add to SQLCJ's post, you will need to include the ORDER BY date_value at the end to get the OP's expected result - and some ugly string manipulation for the date

SELECT CONVERT(CHAR(4), GETDATE(), 120) + ' ' + CONVERT(CHAR(4), GETDATE(), 100) ,
monthly_total ,
( SELECT SUM(monthly_total)
FROM #sales s
WHERE date_value <= tst.date_value
) AS monthly_total
FROM ( SELECT * ,
ROW_NUMBER() OVER ( ORDER BY date_value ) tst
FROM #sales
) tst
ORDER BY date_value A



Ah, be carefull now. That contains an (X2+X)/2+X Triangular join. In this case, it generates 324 internal rows for the 24 original rows. While it "runs fast" for such a small quantity of rows, these things can be server killers. With just 10,000 original rows, SQL Server will generate 50,015,000 internal rows and all of the logical reads to go with it. A cursor and WHILE loop or a Recursive CTE would actually run much faster.

Please see the following article about "Triangular Joins" for more information.
http://www.sqlservercentral.com/articles/T-SQL/61539/

OR... if you follow the rules, there's a bit of undocumented code known at the "Quirky Update". If you don't follow the rules, it can bite ya. It will, however, do a million row running or grouped running total in just seconds and is faster than even the new methods in SQL Server 2012. Plese see the following article for that.
http://www.sqlservercentral.com/articles/T-SQL/68467/


*** I love that QU ***


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Seattlemsp
Seattlemsp
SSC Rookie
SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)SSC Rookie (32 reputation)

Group: General Forum Members
Points: 32 Visits: 64
Thank you all. It helps a lot!!!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search