T-SQL Two Dimension Pivot Table
將原始資料 select BRAND='ASUS', QTY=1, AMT=200 union all select BRAND='BENQ', QTY=1, AMT=200 union all select BRAND='ASUS', QTY=1, AMT=200 union all select BRAND='BENQ', QTY=1, AMT=200 union all select BRAND='MSI', QTY=1, AMT=200 依各品牌匯總數量、金額,做出以下的統計表 解決方法 1 - Group By 加條件式 select ASUS_QTY = SUM(IIF(Src.BRAND='ASUS', Src.QTY, 0)) , ASUS_AMT = SUM(IIF(Src.BRAND='ASUS', Src.AMT, 0)) , BENQ_QTY = SUM(IIF(Src.BRAND='BENQ', Src.QTY, 0)) , BENQ_AMT = SUM(IIF(Src.BRAND='BENQ', Src.AMT, 0)) , MSI_QTY = SUM(IIF(Src.BRAND='MSI', Src.QTY, 0)) , MSI_AMT = SUM(IIF(Src.BRAND='MSI', Src.AMT, 0)) from( select BRAND='ASUS', QTY=1, AMT=200 union all select BRAND='BENQ', QTY=1, AMT=200 union all select BRAND='ASUS', QTY=1, AMT=200 union all select BRAND='BENQ', QTY=1, AMT=200 union all select BRAND='MSI', QTY=1, AMT=200 ) Src 解決方法 2 - Pivot Table 做多維度 Pivot Table