若要將一個明細資料,如下圖
轉換成多維度的彙整資料 ,如下圖
--範例一語法如下: select DepartNo,[0501],[0502],[0503],[0504],[0505],[0506],[0507]--要匯總的類別與分類 from (select DepartNo,PType,BalAmt--明細資料 from #CosUBal_Depart_Tmp where IOType in ('A03','B01','B02','B03','B07')) as S PIVOT (SUM(BalAmt)--要統計的欄位 for PType IN ([0501],[0502],[0503],[0504],[0505],[0506],[0507])) as Piv order by DepartNo
--範例二語法如下: select *--要匯總的類別與分類 from (select DepartNo,PType,BalAmt--明細資料 from #CosUBal_Depart_Tmp where IOType in ('A03','B01','B02','B03','B07')) as S PIVOT (SUM(BalAmt)--要統計的欄位 for PType IN ([0501],[0502],[0503],[0504],[0505],[0506],[0507])) as Piv order by DepartNo以上兩種寫法出來的結果如上第二張圖!
參考自:取之於網路用之於網路-只寫程式,TechNet
沒有留言:
張貼留言