SQL Server Sum results from a pivot to get monthly sales of produtcs

yws3nbqq  于 5个月前  发布在  其他
关注(0)|答案(2)|浏览(42)

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.

https://dbfiddle.uk/oIclJV3u

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:

yearitemcodeNameJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecember
202301product1NULL1051NULL1025109101
202302product25100NULL651032NULL8896462
202303product36NULLNULLNULLNULL20NULL122NULLNULLNULL
202304product4NULLNULLNULLNULLNULLNULL6NULLNULLNULLNULLNULL
202305product51025610541121NULLNULLNULL

Desired result:

yearitemcodeNameJanuaryFebruaryMarchAprilMayJuneJulyAugustSeptemberOctoberNovemberDecemberTotal Sales
202301product1NULL1051NULL102510910191
202302product25100NULL651032NULL8896462454
202303product36NULLNULLNULLNULL20NULL122NULLNULLNULL40
202304product4NULLNULLNULLNULLNULLNULL6NULLNULLNULLNULLNULL6
202305product51025610541121NULLNULLNULL92
cclgggtu

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.

drop table if exists #sales
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
       UNION ALL 
       SELECT YEAR(SalesDate) [YEAR], 
              'Total' [Month], 
              COUNT(1) [Amount Count], 
              itemcode, 
              Name 
       FROM #Sales
       GROUP BY YEAR(SalesDate), itemcode, Name  
       ) AS MonthlyDates
  PIVOT (SUM([Amount Count])
   FOR MONTH IN ([January],[February],[March],[April],[May],
      [June],[July],[August],[September],[October],[November],
      [December],
      [Total])
  ) as MNAMEPIVOT

Here's the result

b4wnujal

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:

alter table sales add month int;

The month can then be extracted:

update sales set month=extract(month from salesdate);

Sales can then be grouped by month and name. Based on the sample data you provided, the results are as follows:

select month, name, sum(amount)
from sales
group by month, name
order by month;
month |   name   |  sum  
-------+----------+-------
    10 | product1 |  1.00
    11 | product1 | 15.00
(2 rows)

相关问题