Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to add up the monthly total? Expand / Collapse
Author
Message
Posted Friday, March 7, 2014 10:04 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, March 12, 2014 2:06 PM
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!

Post #1548800
Posted Friday, March 7, 2014 10:53 AM This worked for the OP Answer marked as solution
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 2:39 AM
Points: 470, Visits: 483
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

Post #1548826
Posted Friday, March 7, 2014 11:03 AM This worked for the OP Answer marked as solution


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Yesterday @ 10:38 PM
Points: 3,931, Visits: 7,160
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"
Post #1548829
Posted Friday, March 7, 2014 11:19 AM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Tuesday, September 16, 2014 2:39 AM
Points: 470, Visits: 483
I was about to update the post and saw your comment Thank you MyDoggieJessie for pointing that out.
Post #1548836
Posted Friday, March 7, 2014 4:37 PM This worked for the OP Answer marked as solution


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 9:16 PM
Points: 37,102, Visits: 31,655
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."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1548926
Posted Sunday, March 9, 2014 6:18 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 3:25 AM
Points: 3,648, Visits: 5,328
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!
Post #1549086
Posted Wednesday, March 12, 2014 2:08 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, March 12, 2014 2:06 PM
Points: 32, Visits: 64
Thank you all. It helps a lot!!!
Post #1550420
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse