Is it possible to collapse the date, month and year with GROUPING SETS, where the dates are displayed and grouped by month and year, and instead of the month null, the value of the month and year is displayed?
Here is sample data and my attempt to use grouping sets
create table
(
employeeId int
,productLine int
,salesDate datetime
,salesTotal money
)
insert into
values
(1,1,'02/09/2017',199)
,(1,1,'04/10/2017',379)
,(2,1,'04/21/2017',323)
,(2,1,'04/10/2017',461)
,(2,1,'06/11/2017',304)
,(3,1,'01/28/2017',147)
,(3,1,'02/09/2017',320)
,(4,1,'03/07/2017',344)
,(4,1,'03/13/2017',176)
select
productLine,
employeeId,
salesDate,
mon=month(salesdate),
yr=year(salesdate),
sum(salesTotal) as salesTotal
from
group by grouping sets
(
(productLine, employeeId, salesDate)
,(productLine, employeeId)
,(productLine)
)
This is what the request returned (left) and what I wanted to fulfill (right)
