如何按第 1 周、第 2 周、第 3 周等旋转下表。每个月?谢谢。
例如数据库表:
这是我需要的表:
这就是我所做的,但我需要更有效的方法来做到这一点。
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