SUM OVER PARTITION计算运行总计

时间:2016-07-11 16:51:51

标签: tsql sql-server-2014

我正在尝试修改我的查询,以在报告中包含每个县的运行总计。以下是我的工作查询,尝试使用SUM OVER PARTITION注释掉:

SELECT  DATEPART(MONTH, r.received_date) AS [MonthID] ,
    DATENAME(MONTH, r.received_date) AS [Month] ,
    o.name AS [CountyName] ,
    rsc.description AS [Filing] ,
    COUNT(r.id) AS [Request_Total] ,
    CAST (AVG(CAST (DATEDIFF(HOUR, received_date, completion_date) AS DECIMAL(8,2))) / 24 AS DECIMAL(8,2)) AS [Total_Time_Days] 
    --SUM(r.id) OVER (PARTITION BY o.name) AS [TotalFilings]
FROM    dbo.requests AS [r]
    INNER JOIN dbo.organizations AS [o] ON o.id = r.submitted_to_organiztion_id
    INNER JOIN dbo.request_status_codes AS [rsc] ON rsc.code = r.request_status_code
WHERE   r.submitted_to_organiztion_id < 68
    AND r.request_type_code = 1
    AND CAST(r.received_date AS DATE) >= '01/01/2016'
    AND CAST(r.received_date AS DATE) <= '06/30/2016'
    AND o.name = 'Alachua'
GROUP BY DATENAME(MONTH, r.received_date) ,
    DATEPART(MONTH, r.received_date) ,
    o.name ,
    rsc.description
ORDER BY DATEPART(MONTH, r.received_date) ,
    CountyName ,
    Filing;

结果看起来是正确的:

Result Set

也许我在滥用SUM PARTITION BY,但我的最终目标是添加一个额外的列,按月对每个县的归档类型求和。

例如,1月份的额外列应为13,654,而2月应为14,238,依此类推。

我可以就如何使此查询正常工作获得一些建议吗?谢谢,

1 个答案:

答案 0 :(得分:0)

Not sure this is the best way or more efficient, but I was able to create a sub-query to obtain the results I wanted. I do believe a CTE or use of a Windows function would be better, but I haven't been able to get it to work. Here is my query however:

SELECT  X.[MonthID] ,
    X.[Month] ,
    X.[CountyName] ,
    X.[Filing] ,
    X.[Avg_Time_Days] ,
    SUM(X.Request_Total) AS [Total_Requests]
FROM    ( SELECT    DATEPART(MONTH, r.received_date) AS [MonthID] ,
                DATENAME(MONTH, r.received_date) AS [Month] ,
                o.name AS [CountyName] ,
                rsc.description AS [Filing] ,
                COUNT(r.id) AS [Request_Total] ,
                CAST (AVG(CAST (DATEDIFF(HOUR, received_date,
                                         completion_date) AS DECIMAL(8, 2)))
                / 24 AS DECIMAL(8, 2)) AS [Avg_Time_Days]
    --, SUM(r.id) OVER (PARTITION BY o.name, rsc.description) AS [TotalFilings]
      FROM      dbo.requests AS [r]
                INNER JOIN dbo.organizations AS [o] ON o.id = r.submitted_to_organiztion_id
                INNER JOIN dbo.request_status_codes AS [rsc] ON rsc.code = r.request_status_code
      WHERE     r.submitted_to_organiztion_id < 68
                AND r.request_type_code = 1
                AND CAST(r.received_date AS DATE) >= '01/01/2016'
                AND CAST(r.received_date AS DATE) <= '06/30/2016'
    --AND o.name = 'Alachua'
GROUP BY            DATENAME(MONTH, r.received_date) ,
                DATEPART(MONTH, r.received_date) ,
                o.name ,
                rsc.description
    --, r.id
--ORDER BY DATEPART(MONTH, r.received_date) ,
--        CountyName ,
--        Filing
    ) AS X
GROUP BY X.[MonthID] ,
    X.[Month] ,
    X.[CountyName] ,
    X.[Filing] ,
    X.[Avg_Time_Days]
ORDER BY X.[MonthID] ,
    X.[Month] ,
    X.[CountyName] ,
    X.[Filing];