在SQL Server中包含多个字段的数据透视表

时间:2016-06-08 08:39:32

标签: sql sql-server pivot aggregate-functions

我有这样的表格。

|DateID      |DivisionCode | TransactionIn| TransactionOut| OutstandingTransaction|
-----------------------------------------------------------------------------------
|2016-01-01  |ENP          | 10           | 10            | 0                     |
|2016-08-01  |PA           | 2            | 0             | 2                     |
|2016-08-08  |MD           | 3            | 3             | 0                     |

我使用它来获取每个月的总TransactionIn,TransactionOut和OutstandingTransaction。

;WITH MissingMonths
AS 
    (
        SELECT number
        FROM master..spt_values
        WHERE number > 0 AND number < 13 
        GROUP BY number
    ),
RPT_POApproval
AS (
        SELECT
        MONTH(RPT.DateID) as DateID,
        SUM(RPT.TransactionIn) as TransactionIn,
        SUM(RPT.TransactionOut) as TransactionOut,
        SUM(RPT.OutstandingTransaction) as OutstandingTransaction
        FROM RPT_SummaryPOApproval RPT
        WHERE RPT.Deleted = 0
        GROUP BY MONTH(DateID)
    )

SELECT COALESCE(number,DateID) AS Month
      ,COALESCE(TransactionIn, 0) AS TransactionIn
      ,COALESCE(TransactionOut, 0) AS TransactionOut
      ,COALESCE(OutstandingTransaction, 0) AS OutstandingTransaction
FROM MissingMonths MM 
LEFT JOIN RPT_POApproval R ON MM.number = R.DateID

所以看起来像这样

|Month | TransactionIn| TransactionOut| OutstandingTransaction|
-------------------------------------------------------------------
|1     | 12           | 10            | 2                     |
|2     | 0            | 0             | 0                     |
|3     | 0            | 0             | 0                     |
|4     | 0            | 0             | 0                     |
|5     | 0            | 0             | 0                     |
|6     | 0            | 0             | 0                     |
|7     | 0            | 0             | 0                     |
|8     | 3            | 3             | 0                     |
|9     | 0            | 0             | 0                     |
|10    | 0            | 0             | 0                     |
|11    | 0            | 0             | 0                     |
|12    | 0            | 0             | 0                     |

如何使用pivot获取结果,以便数据看起来像这样?

|1 |2 |3 |4 |5 |6 |7 |8 |9 |10 |11 |12 |
-----------------------------------------
|12|0 |0 |0 |0 |0 |0 |3 |0 |0  |0  |0  |  >> It's for Transaction In
|10|0 |0 |0 |0 |0 |0 |3 |0 |0  |0  |0  |  >> It's for Transaction Out
|2 |0 |0 |0 |0 |0 |0 |3 |0 |0  |0  |0  |  >> It's for Outstanding Trans

1 个答案:

答案 0 :(得分:0)

首先是UNPIVOT然后是PIVOT:)

;WITH cte AS (
SELECT COALESCE(number,DateID) AS Month
      ,COALESCE(TransactionIn, 0) AS TransactionIn
      ,COALESCE(TransactionOut, 0) AS TransactionOut
      ,COALESCE(OutstandingTransaction, 0) AS OutstandingTransaction
FROM MissingMonths MM 
LEFT JOIN RPT_POApproval R ON MM.number = R.DateID
)

SELECT *
FROM (
    SELECT [Month], [Transactions], [Values]
    FROM (
        SELECT *
        FROM cte
    ) as p
    UNPIVOT (
        [Values] FOR [Transactions] IN (TransactionIn, TransactionOut, OutstandingTransaction)
    ) as unpvt
) as k
PIVOT (
    MAX([Values]) FOR [Month] IN ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])
) as pvt

输出:

Transactions            1   2   3   4   5   6   7   8   9   10  11  12
TransactionIn           12  0   0   0   0   0   0   3   0   0   0   0
TransactionOut          10  0   0   0   0   0   0   3   0   0   0   0
OutstandingTransaction  2   0   0   0   0   0   0   0   0   0   0   0