I'm writing a query to get results of monthly sales, I'm just missing a column to sum the total sales of all months.
Query:
create table sales(
itemcode INT, name varchar(255), amount decimal(18,2), salesdate date);
insert into sales (itemcode, name, amount, salesdate)
values
(01, 'product1', 01, '2023-10-01'),
(01, 'product1', 5, '2023-11-03'),
(02, 'product1', 10, '2023-11-10')
SELECT * FROM (SELECT YEAR(SalesDate) [YEAR], DATENAME(MONTH, SalesDate) [Month], COUNT(1) [Amount Count], itemcode, Name FROM Sales
GROUP BY YEAR(SalesDate), DATENAME(MONTH, SalesDate), itemcode, Name) AS MonthlyDates
PIVOT (SUM([Amount COUNT])
FOR MONTH IN ([January],[February],[March],[April],[May],
[June],[July],[August],[September],[October],[November],
[December])) as MNAMEPIVOT
Results in this:
year | itemcode | Name | January | February | March | April | May | June | July | August | September | October | November | December |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2023 | 01 | product1 | NULL | 10 | 5 | 1 | NULL | 10 | 2 | 5 | 10 | 9 | 10 | 1 |
2023 | 02 | product2 | 5 | 100 | NULL | 65 | 10 | 32 | NULL | 8 | 89 | 6 | 46 | 2 |
2023 | 03 | product3 | 6 | NULL | NULL | NULL | NULL | 20 | NULL | 12 | 2 | NULL | NULL | NULL |
2023 | 04 | product4 | NULL | NULL | NULL | NULL | NULL | NULL | 6 | NULL | NULL | NULL | NULL | NULL |
2023 | 05 | product5 | 10 | 2 | 5 | 6 | 10 | 5 | 41 | 12 | 1 | NULL | NULL | NULL |
Desired result:
year | itemcode | Name | January | February | March | April | May | June | July | August | September | October | November | December | Total Sales |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
2023 | 01 | product1 | NULL | 10 | 5 | 1 | NULL | 10 | 2 | 5 | 10 | 9 | 10 | 1 | 91 |
2023 | 02 | product2 | 5 | 100 | NULL | 65 | 10 | 32 | NULL | 8 | 89 | 6 | 46 | 2 | 454 |
2023 | 03 | product3 | 6 | NULL | NULL | NULL | NULL | 20 | NULL | 12 | 2 | NULL | NULL | NULL | 40 |
2023 | 04 | product4 | NULL | NULL | NULL | NULL | NULL | NULL | 6 | NULL | NULL | NULL | NULL | NULL | 6 |
2023 | 05 | product5 | 10 | 2 | 5 | 6 | 10 | 5 | 41 | 12 | 1 | NULL | NULL | NULL | 92 |
2条答案
按热度按时间cclgggtu1#
This is not the most elegant way of doing it and to be honest this kind of things should really be done in the presentation layer (e.g. in your report) but anyway..
You can just UNION the year totals in your subquery can fix the name as 'Total' then just add this new month to the pivot expression.
Here's the result
b4wnujal2#
One solution is to extract the month from the salesdate variable, and then use a GROUP BY function to sum up the sales amount by month.
Firstly, a separate variable can be created as follows:
The month can then be extracted:
Sales can then be grouped by month and name. Based on the sample data you provided, the results are as follows: