March 7, 2014 at 10:04 am
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?
YearMonthMonthlySaleAccumulated Total
2012Jan110 110
2012Feb130 240
2012Mar30 270
2012Apr60 330
2012May10 340
2012Jun30 370
2012Jul20 390
2012Aug150 540
2012Sep170 710
2012Oct30 740
2012Nov100 840
2012Dec80 920
2013Jan100 1020
2013Feb90 1110
2013Mar120 1230
2013Apr200 1430
2013May200 1630
2013Jun300 1930
2013Jul1500 3430
2013Aug600 4030
2013Sep200 4230
2013Oct800 5030
2013Nov500 5530
2013Dec50 5580
Thanks!
March 7, 2014 at 10:53 am
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
March 7, 2014 at 11:03 am
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; Theyll drag you down to their level and beat you with experience
March 7, 2014 at 11:19 am
I was about to update the post and saw your comment Thank you MyDoggieJessie for pointing that out.
March 7, 2014 at 4:37 pm
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
Change is inevitable... Change for the better is not.
March 9, 2014 at 6:18 pm
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.
*** I love that QU ***
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?[/url]
Since random numbers are too important to be left to chance, let's generate some![/url]
Learn to understand recursive CTEs by example.[/url]
[url url=http://www.sqlservercentral.com/articles/St
March 12, 2014 at 2:08 pm
Thank you all. It helps a lot!!!
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply