2016-07-11 22 views
0

レポートに各郡の合計を含めるようにクエリを変更しようとしています。実行中の合計を計算するために分割した合計

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; 

そして結果が正しく見える::以下はコメントアウトSUM OVER PARTITIONを使用しようと私の作業クエリです

Result Set

おそらく、私はSUM PARTITION BYを悪用していますが、私の最終目標は、ANを追加することです月ごとに各郡の申告タイプを合計する追加の列。

たとえば、1月の追加列は13,654、2月は14,238などとなります。

このクエリを正しく動作させる方法についていくつかアドバイスをいただけますか?ありがとう、

+0

[SQL Serverで実行中の合計を計算]の可能複製(http://stackoverflow.com/questions/860966/calculate-a-running-total-in-sql-server ) – dfundako

+0

レコード1〜4では、 'Total Filings'列の値が70/13654/13698/27892であるか、70/13654/44/14238であると予想されますか? – tarheel

+0

これは間違いありません。@タールヒル – MISNole

答えて

0

これが最良の方法であるか、より効率的かはわかりませんが、私が望む結果を得るためにサブクエリを作成することができました。私はCTEやWindowsの機能の使用が良いと信じていますが、私はそれを動作させることができませんでした。ここに私のクエリはしかし、次のとおりです。

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]; 
関連する問題