我有这样的查询:
`SELECT [/BIC/IORSVPTX] as Region, COUNTRY_ID, [/BIC/IOWCNTRY] as Country, [/BIC/IOC_TRLNO] as Trial, [/BIC/IOWQUAL] as ResourceType, case when [/BIC/IOWQUAL] like '%Supporter%' then 1 when [/BIC/IOWQUAL] like '%Monitor%' then 3 when [/BIC/IOWQUAL] like '%PM%' then 2 end as ResourceGroup, [1], [2], [3], [4], [5] FROM ( SELECT [/BIC/IORSVPTX], COUNTRY_ID, [/BIC/IOWCNTRY], [/BIC/IOC_TRLNO], [/BIC/IOWQUAL], case when [/BIC/IOWQUAL] like '%Supporter%' then 1 when [/BIC/IOWQUAL] like '%Monitor%' then 3 when [/BIC/IOWQUAL] like '%PM%' then 2 end as ResourceGroup, left(CALMONTH,4) as StartYear, right(CALMONTH,2) as StartMonth, ((left(CALMONTH,4) - 2013) * 12) + right(CALMONTH,2) AS YearMonth, QUANTITY as Hours FROM dbo.Actuals where [/BIC/IOC_TRLNO]<>'0000' and left(CALMONTH,4)>2012 and COUNTRY_ID='10' and ([/BIC/IOWQUAL] like '%PM%' or [/BIC/IOWQUAL] like'%Monitor%' or [/BIC/IOWQUAL] like '%Supporter%') ) up PIVOT (sum(Hours) FOR YearMonth IN ([1],[2],[3],[4],[5])) AS pvt; IN ([1],[2],[3],[4],[5])) AS pvt;`
这为我提供了每个 ResourceType 和 ResourceGroup 2 行的输出,其中 1(1 月)的小时数和第 2 行的小时数为 2(2 月)而不是 1 行
Region COUNTRY_ID Country Trial ResourceType ResourceGroup 1 2 3 4 5
North America 10 USA 3619 Monitor 3 158.5 NULL NULL NULL NULL
North America 10 USA 3619 Monitor 3 NULL 42 NULL NULL NULL
North America 10 USA 3619 PM / LTM / RTM 2 20 NULL NULL NULL NULL
North America 10 USA 3619 PM / LTM / RTM 2 NULL 22 NULL NULL NULL
North America 10 USA 3619 Supporter 1 18.5 NULL NULL NULL NULL
North America 10 USA 3619 Supporter 1 NULL 15.75 NULL NULL NULL
所需的输出应该是这样的:
Region COUNTRY_ID Country Trial ResourceType ResourceGroup 1 2 3 4 5
North America 10 USA 3619 Monitor 3 158.5 42 NULL NULL NULL
North America 10 USA 3619 PM / LTM / RTM 2 20 22 NULL NULL NULL
North America 10 USA 3619 Supporter 1 18.5 15.75 NULL NULL NULL
我会感谢你的帮助!