SQL查询PIVOT

时间:2019-03-06 10:20:56

标签: sql-server tsql pivot

我想按期间排列OrderValue的总和。现在,我的SQL查询以表格格式显示,我希望它在一行中。如果OrderValue当前处于当前状态,则应在第0列下;如果下个月到期,则必须在第1列下,依此类推。请查看我的SQL查询

ALTER PROCEDURE [dbo].[sp_GetInvoicedPayments]
@CustomerID int   
AS
BEGIN   
DECLARE @endOfCurrentMonth DATE = EOMONTH(GETDATE())

SELECT [data].CustomerID, [data].[Period], SUM([data].OrderValue) AS 
OrderValue 
FROM (
SELECT pms.CustomerID, pms.OrderValue,
        CASE
            WHEN ((SELECT CAST(pms.ExpectedDate AS DATE)) <= paymentInfo.CurrentDueMonth) THEN 0
            WHEN ((SELECT CAST(pms.ExpectedDate AS DATE)) <= DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0, DATEADD(MONTH, 1, paymentInfo.CurrentDueMonth)) + 1,- 1)) ) THEN 1
            WHEN ((SELECT CAST(pms.ExpectedDate AS DATE)) <= DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0, DATEADD(MONTH, 2, paymentInfo.CurrentDueMonth)) + 1,- 1))) THEN  2
            WHEN ((SELECT CAST(pms.ExpectedDate AS DATE)) <= DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0, DATEADD(MONTH, 3, paymentInfo.CurrentDueMonth)) + 1,- 1))) THEN  3
            WHEN ((SELECT CAST(pms.ExpectedDate AS DATE)) <= DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0, DATEADD(MONTH, 4, paymentInfo.CurrentDueMonth)) + 1,- 1))) THEN  4
        END AS [Period]
FROM PaymentMilestoneSummary pms
INNER JOIN (
    SELECT cus.ID AS CustomerID,
    CASE 
        WHEN cus.PaymentStatusID = 1 THEN @endOfCurrentMonth
        WHEN cus.PaymentStatusID = 2 THEN (SELECT CAST(DATEADD(month, - 1, @endOfCurrentMonth) AS DATE))
        WHEN cus.PaymentStatusID = 3 THEN (SELECT CAST(DATEADD(month, - 2, @endOfCurrentMonth) AS DATE))
        WHEN cus.PaymentStatusID = 4 THEN (SELECT CAST(DATEADD(month, - 3, @endOfCurrentMonth) AS DATE))
        WHEN cus.PaymentStatusID = 5 THEN (SELECT CAST(DATEADD(month, - 4, @endOfCurrentMonth) AS DATE))
    END AS CurrentDueMonth
    FROM Company cus 
) paymentInfo ON pms.CustomerID = paymentInfo.CustomerID AND paymentInfo.CustomerID= @CustomerID
)[data]
 GROUP BY [data].CustomerID, [data].[Period]
END

这就是我得到的:

enter image description here

这是我想要的样子的一个例子:

An example of how I want it to be

1 个答案:

答案 0 :(得分:3)

您可以像使用PIVOTCTE

一样进行操作
;with cte as
(
 --wrap you existing query
)

SELECT   
[0], [1], [2], [3], [4]  
FROM  
(select Period, OrderValue from cte) AS SourceTable  
PIVOT  
(  
    max(OrderValue)  
    FOR Period IN ([0], [1], [2], [3], [4])  
) AS PivotTable;  

您的过程应如下所示。

ALTER PROCEDURE [dbo].[sp_GetInvoicedPayments]
@CustomerID int   
AS
BEGIN   
DECLARE @endOfCurrentMonth DATE = EOMONTH(GETDATE())
;with CTE AS
(
    SELECT [data].CustomerID, [data].[Period], SUM([data].OrderValue) AS 
    OrderValue 
    FROM (
    SELECT pms.CustomerID, pms.OrderValue,
            CASE
                WHEN ((SELECT CAST(pms.ExpectedDate AS DATE)) <= paymentInfo.CurrentDueMonth) THEN 0
                WHEN ((SELECT CAST(pms.ExpectedDate AS DATE)) <= DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0, DATEADD(MONTH, 1, paymentInfo.CurrentDueMonth)) + 1,- 1)) ) THEN 1
                WHEN ((SELECT CAST(pms.ExpectedDate AS DATE)) <= DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0, DATEADD(MONTH, 2, paymentInfo.CurrentDueMonth)) + 1,- 1))) THEN  2
                WHEN ((SELECT CAST(pms.ExpectedDate AS DATE)) <= DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0, DATEADD(MONTH, 3, paymentInfo.CurrentDueMonth)) + 1,- 1))) THEN  3
                WHEN ((SELECT CAST(pms.ExpectedDate AS DATE)) <= DATEADD(s,0,DATEADD(mm, DATEDIFF(m,0, DATEADD(MONTH, 4, paymentInfo.CurrentDueMonth)) + 1,- 1))) THEN  4
            END AS [Period]
    FROM PaymentMilestoneSummary pms
    INNER JOIN (
        SELECT cus.ID AS CustomerID,
        CASE 
            WHEN cus.PaymentStatusID = 1 THEN @endOfCurrentMonth
            WHEN cus.PaymentStatusID = 2 THEN (SELECT CAST(DATEADD(month, - 1, @endOfCurrentMonth) AS DATE))
            WHEN cus.PaymentStatusID = 3 THEN (SELECT CAST(DATEADD(month, - 2, @endOfCurrentMonth) AS DATE))
            WHEN cus.PaymentStatusID = 4 THEN (SELECT CAST(DATEADD(month, - 3, @endOfCurrentMonth) AS DATE))
            WHEN cus.PaymentStatusID = 5 THEN (SELECT CAST(DATEADD(month, - 4, @endOfCurrentMonth) AS DATE))
        END AS CurrentDueMonth
        FROM Company cus 
    ) paymentInfo ON pms.CustomerID = paymentInfo.CustomerID AND paymentInfo.CustomerID= @CustomerID
    )[data]
     GROUP BY [data].CustomerID, [data].[Period]
)

SELECT [0], [1], [2], [3], [4]  
FROM  
(select Period, OrderValue from cte) AS SourceTable  
PIVOT  
(  
    max(OrderValue)  
    FOR Period IN ([0], [1], [2], [3], [4])  
) AS PivotTable; 

END

编辑:

  

然后我该如何总结从那一行获得的那些值?

总而言之,您可以像下面那样更改数据透视查询。

SELECT   
[0], [1], [2], [3], [4] , s as [Sum]
FROM  
(select Period, OrderValue, sum(OrderValue) over() s from cte) AS SourceTable  
PIVOT  
(  
    max(OrderValue)  
    FOR Period IN ([0], [1], [2], [3], [4])  
) AS PivotTable;