需求 : 行列转换
实现SQL :
SELECT *
FROM ( SELECT Item , Grp = 'Overoll' , Num = [Overoll] FROM #test
UNION ALL
SELECT Item , Grp = 'I_Branch' , Num = [I_Branch] FROM #test
UNION ALL
SELECT Item , Grp = 'P_Branch' , Num = [P_Branch] FROM #test
UNION ALL
SELECT Item , Grp = 'CNG' , Num = [CNG] FROM #test
UNION ALL
SELECT Item , Grp = 'Transmitter' , Num = [Transmitter] FROM #test
) t PIVOT ( MAX(Num) FOR Item IN ( [Booking], [Shpment], [OD_CRD], [OD_PD] ) ) pivt
UNION ALL
SELECT Item , Grp = 'I_Branch' , Num = [I_Branch] FROM #test
UNION ALL
SELECT Item , Grp = 'P_Branch' , Num = [P_Branch] FROM #test
UNION ALL
SELECT Item , Grp = 'CNG' , Num = [CNG] FROM #test
UNION ALL
SELECT Item , Grp = 'Transmitter' , Num = [Transmitter] FROM #test
) t PIVOT ( MAX(Num) FOR Item IN ( [Booking], [Shpment], [OD_CRD], [OD_PD] ) ) pivt