I am trying to create a pivot in order to sum specific month as the total, below is my table and sql pivot:
CREATE TABLE yt
([Store] varchar(10), [Month] varchar(10), [xCount] int)
;
INSERT INTO yt
([Store], [Month], [xCount])
VALUES
('A', 'JAN', 1),
('A', 'FEB', 10),
('B', 'JAN', 2),
('B', 'FEB', 20),
('C', 'JAN', 3),
('C', 'FEB', 70),
('C', 'MAR', 110)
;
select *
from
(
select Store, Month, xCount
from yt
) src
pivot
(
sum(xcount)
for Month in ([JAN], [FEB], [MAR])
) piv;
Store JAN FEB MAR
A 1 10 NULL
B 2 20 NULL
C 3 70 110
The above result I am able to sum the value by the current month. Now I want to sum all the month when Store a, sum only FEB, and MAR in Store b, and Store c sum MAR, so I want my result as
Store JAN FEB MAR
A 6 100 110
B 5 90 110
C 3 70 110
Can I do it in pivot? Or need to create a table to run sql again? Thanks for the help.