SQL Server-使用PIVOT函数

时间:2018-09-27 15:48:11

标签: sql sql-server pivot ssms

我需要帮助弄清楚如何在此问题的SQL查询中添加PIVOT函数。

查找过去12个月内每个州(状态)在其SLA中每月处理的索赔的百分比。

这是我到目前为止所拥有的...

SELECT cs.ERRetroResponseStatusId
    , DATENAME(MONTH, c.CreatedDate) [Month]
    , SUM(CASE
            WHEN ERRetroResponseStatusId = 1 and (DATEDIFF(HOUR, c.CreatedDate, GETDATE()))<24 THEN 1 
        WHEN ERRetroResponseStatusId = 3 and (DATEDIFF(DAY, c.CreatedDate, GETDATE()))<45 THEN 1 
        ELSE 0 END) AS CountOfGroupedStatusIDs
    , COUNT(cs.ERRetroResponseStatusId) AS TotalAmountOfClaimsInStatus
    , ROUND(100.00 * COUNT(cs.ERRetroResponseStatusId) / SUM(COUNT(*)) OVER(),2) AS [Percentage]
--convert(decimal(18,2),x) 
FROM ERRetroClaimResponse cs
    INNER JOIN ERRetroState s ON cs.ERRetroResponseStatusId = s.Id
    LEFT JOIN ERRetroClaim c ON c.Id = cs.ERRetroClaimId
WHERE cs.CreatedDate BETWEEN DATEADD(MONTH, -12, GETDATE()) AND GETDATE() 
and cs.ERRetroResponseStatusId in (1,3)
GROUP BY cs.ERRetroResponseStatusId, DATENAME(MONTH, c.CreatedDate)
Id   ** ClaimID ** StatusID **      CreatedDate        ** CreatedBy
===========================================================================
1174 **   977   **    1     ** 2018-06-01 18:01:31.387 **     1
---------------------------------------------------------------------------
1175 **   978   **    1     ** 2018-06-01 18:01:31.387 **     1
---------------------------------------------------------------------------
1176 **   979   **    1     ** 2018-06-01 18:01:31.387 **     1
---------------------------------------------------------------------------
1177 **   980   **    1     ** 2018-06-01 18:01:31.403 **     1
---------------------------------------------------------------------------
1178 **   981   **    1     ** 2018-06-01 18:01:31.403 **     1
---------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

此查询会将您的状态ID转换为列名,月份以行值列出。

;WITH AggData AS
(
    SELECT cs.ERRetroResponseStatusId
        , DATENAME(MONTH, c.CreatedDate) AS [Month]
        , SUM(CASE
                WHEN ERRetroResponseStatusId = 1 and (DATEDIFF(HOUR, c.CreatedDate, GETDATE()))<24 THEN 1 
            WHEN ERRetroResponseStatusId = 3 and (DATEDIFF(DAY, c.CreatedDate, GETDATE()))<45 THEN 1 
            ELSE 0 END) AS CountOfGroupedStatusIDs
        , COUNT(cs.ERRetroResponseStatusId) AS TotalAmountOfClaimsInStatus
    FROM ERRetroClaimResponse cs
    INNER JOIN ERRetroState s ON cs.ERRetroResponseStatusId = s.Id
    LEFT JOIN ERRetroClaim c ON c.Id = cs.ERRetroClaimId
    WHERE cs.CreatedDate BETWEEN DATEADD(MONTH, -12, GETDATE()) AND GETDATE() 
        AND cs.ERRetroResponseStatusId in (1,3)
    GROUP BY cs.ERRetroResponseStatusId, DATENAME(MONTH, c.CreatedDate)
)
SELECT 
    [Month]
    , [1] AS 'For ERRetroResponseStatusId = 1'
    , [3] AS 'For ERRetroResponseStatusId = 3'
FROM
(
    SELECT 
        ERRetroResponseStatusId
        ,[Month]
        , ROUND((CountOfGroupedStatusIDs / TotalAmountOfClaimsInStatus) * 100.00, 2) AS [Percentage]
    FROM AggData
) AS SourceTable
PIVOT
(
    MAX([Percentage])
    FOR ERRetroResponseStatusId IN ([1],[3])
) AS PivotTable

该查询会将月份转换为列名。

;WITH AggData AS
(
    SELECT cs.ERRetroResponseStatusId
        , DATENAME(MONTH, c.CreatedDate) AS [Month]
        , SUM(CASE
                WHEN ERRetroResponseStatusId = 1 and (DATEDIFF(HOUR, c.CreatedDate, GETDATE()))<24 THEN 1 
            WHEN ERRetroResponseStatusId = 3 and (DATEDIFF(DAY, c.CreatedDate, GETDATE()))<45 THEN 1 
            ELSE 0 END) AS CountOfGroupedStatusIDs
        , COUNT(cs.ERRetroResponseStatusId) AS TotalAmountOfClaimsInStatus
    FROM ERRetroClaimResponse cs
    INNER JOIN ERRetroState s ON cs.ERRetroResponseStatusId = s.Id
    LEFT JOIN ERRetroClaim c ON c.Id = cs.ERRetroClaimId
    WHERE cs.CreatedDate BETWEEN DATEADD(MONTH, -12, GETDATE()) AND GETDATE() 
        AND cs.ERRetroResponseStatusId in (1,3)
    GROUP BY cs.ERRetroResponseStatusId, DATENAME(MONTH, c.CreatedDate)
)
SELECT 
    ERRetroResponseStatusId
    ,[January]
    ,[February]
    ,[March]
    ,[April]
    ,[May]
    ,[June]
    ,[July]
    ,[August]
    ,[September]
    ,[October]
    ,[November]
    ,[December]
FROM
(
    SELECT 
        ERRetroResponseStatusId
        ,[Month]
        , ROUND((CountOfGroupedStatusIDs / TotalAmountOfClaimsInStatus) * 100.00, 2) AS [Percentage]
    FROM AggData
) AS SourceTable
PIVOT
(
    MAX([Percentage])
    FOR [Month] IN ([January],[February],[March],[April],[May],[June],[July],[August],[September],[October],[November],[December])
) AS PivotTable