我使用pivot进行了此查询
Select *
From (
Select Quarters = concat('Quarter',DATEPART(QUARTER,EndDate))
,Item = [Name]
,Value = count(*)
From TS
Group By DATEPART(QUARTER,EndDate),[Name]
Union All
Select Quarters
,Item
,Value = 0
From ( values ('Quater1')
,('Quater2')
,('Quater3')
,('Quater4')
) A(Quarters)
Cross Join ( Select distinct Item=[Name] from TS) B
) src
Pivot ( sum(Value) for Item in ([Status1],[Status2],[Status3],[Status4]) ) pvt
我需要在select中得到这个例子中的数据
Quarters Status1 Status2 Status3 Status4
Quater1 0 0 0 0
Quater2 0 0 0 0
Quater3 0 0 0 0
Quater4 0 0 0 0
但是数据选择就像这个例子
Quarters Status1 Status2 Status3 Status4
Quarter1 NULL 1 NULL 1
Quarter3 NULL NULL NULL 1
Quarter4 NULL NULL NULL 2
Quater1 0 0 0 0
Quater2 0 0 0 0
Quater3 0 0 0 0
Quater4 0 0 0 0
有人有什么建议吗?
1条答案
按热度按时间cnwbcb6i1#
修正了四分之一对四分之一的打字错误(缺少r)