(MSSQL)資料轉置PIVOT(Row2Col)
Syntax
1 | SELECT <non-pivoted column>, |
案例
資料表 Customer_Order 內容如下:
客戶每月訂單總數
1 | SELECT YEAR(pvt.OrderDate) AS [Year], MONTH(pvt.OrderDate) AS [MONTH], SUM(pvt.CustomerA) AS CustomerA, SUM(pvt.CustomerB) AS CustomerB |
客戶每月下單總量
1 | SELECT [YEAR],[MONTH],ISNULL([CustomerA],0) AS CustomerA, ISNULL([CustomerB],0) AS CustomerB |
客戶每次下單均量
1 | SELECT [YEAR],ISNULL([CustomerA],0) AS CustomerA, ISNULL([CustomerB],0) AS CustomerB |