次のテーブルを週 1、週 2、週 3 などでピボットする方法。毎月?ありがとう。
たとえば、db テーブル:
これは私が必要とするテーブルです:
これは私がやったことですが、これを行うにはもっと効率的な方法が必要です。
SELECT 'Oct' AS [Month]
,[Ocd]
,(select Wk_Cmpl from tb1 where WkNum = '1' and RIGHT(wkdt,2) = '10' and Ocd = '167') as wk1
,(select WKLY_PCT from tb1 where WkNum = '1' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as [wk1%]
,(select WE from tb1 where WKNum = '2' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as wk2
,(select WKLY_PCT from tb1 where WkNum = '2' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as [wk2%]
,(select WE from tb1 where WkNum = '3' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as wk3
,(select WKLY_PCT from tb1 where WkNum = '3' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as [wk3%]
,(select WE from tb1 where WkNum = '4' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as wk4
,(select WKLY_PCT from tb1 where WkNum = '4' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as [wk4%]
,(select WE from tb1 where WkNum = '5' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as wk5
,(select WKLY_PCT from tb1 where WkNum = '5' and SUM_LVL_Sort=3.00 and RIGHT(wkdt,2) = '10' and Ocd = '167') as [wk5%]
,[WKLY_AVG] As [Wk Avg]
,[MTH] AS [Mo. Cmpl]
,[COMB_FYTD_COMPLT_ALL] As [M/YTD Total]
,[COMB_FYTD_COMPLT_TARGET_PCT] As [% Goal]
FROM tb1