【问题标题】:sql query PIVOTsql查询PIVOT
【发布时间】:2019-03-06 10:20:56
【问题描述】:

我想按期间排列 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

这是我得到的:

这是我想要的一个例子:

【问题讨论】:

    标签: sql-server tsql pivot


    【解决方案1】:

    您可以使用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;  
    

    【讨论】:

    • 请再做一件事,然后我将如何总结我从那一行得到的值?
    猜你喜欢
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-11
    • 2021-12-25
    相关资源
    最近更新 更多