次のようなクエリがあります。
`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 (1 月) の時間が、2 行目 (2 月) の時間が 2 行に表示されます。
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
私はあなたの助けに感謝します!