我的桌子有以下结构:
PID
Year
Month
LOB
Contracts
现在我希望将一行显示为具有所选LOB的输出,并且它必须具有12列作为输出,每个列用于每个月(今年)。现在我使用的查询太长了,这会增加总时间。任何人都可以提出更好的方法来解决这类问题。
答案 0 :(得分:3)
我认为你正在寻找这个:
TRANSFORM COUNT([Contracts])
SELECT [LOB]
FROM [Table]
WHERE [Year]=2016
AND [LOB]='xxx'
GROUP BY [LOB]
PIVOT [Month] IN (1,2,3,4,5,6,7,8,9,10,11,12)
要回答您的评论,如果您想要2年,请尝试:
TRANSFORM COUNT([Contracts])
SELECT [LOB]
FROM [Table]
WHERE [Year] IN (2015,2016)
AND [LOB]='xxx'
GROUP BY [LOB]
PIVOT ([Year] & '-' & [Month]) IN
(
'2015-1','2015-2','2015-3','2015-4','2015-5','2015-6','2015-7','2015-8','2015-9','2015-10','2015-11','2015-12',
'2016-1','2016-2','2016-3','2016-4','2016-5','2016-6','2016-7','2016-8','2016-9','2016-10','2016-11','2016-12'
)