1

こんにちは、仕事で次のような手順があります

create procedure sp_CountPickupsduringYearReportA1A2
 @MyYear int 
AS
SELECT
MAX(a.AgencyName),
COUNT(PickupID) as YearTotal,
COUNT(PickupID)/(CASE WHEN YEAR(GETDATE()) =@myYear THEN MONTH(GETDATE()) ELSE 12 END) as MoAvg,
SUM(CASE WHEN DATEPART(month, PickupDate) = 1 THEN 1 ELSE 0 end) as Jan,
SUM(CASE WHEN DATEPART(month, PickupDate) = 2 THEN 1 ELSE 0 end) as Feb,
SUM(CASE WHEN DATEPART(month, PickupDate) = 3 THEN 1 ELSE 0 end) as Mar,
SUM(CASE WHEN DATEPART(month, PickupDate) = 4 THEN 1 ELSE 0 end) as Apr, 
SUM(CASE WHEN DATEPART(month, PickupDate) = 5 THEN 1 ELSE 0 end) as May,
SUM(CASE WHEN DATEPART(month, PickupDate) = 6 THEN 1 ELSE 0 end) as Jun,
SUM(CASE WHEN DATEPART(month, PickupDate) = 7 THEN 1 ELSE 0 end) as Jul,
SUM(CASE WHEN DATEPART(month, PickupDate) = 8 THEN 1 ELSE 0 end) as Aug, 
SUM(CASE WHEN DATEPART(month, PickupDate) = 9 THEN 1 ELSE 0 end) as Sep,
SUM(CASE WHEN DATEPART(month, PickupDate) = 10 THEN 1 ELSE 0 end) as Oct,
SUM(CASE WHEN DATEPART(month, PickupDate) = 11 THEN 1 ELSE 0 end) as Nov,
SUM(CASE WHEN DATEPART(month, PickupDate) = 12 THEN 1 ELSE 0 end) as Dec 
FROM dbo.Pickup p join Agency a on p.agencyid = a.agencyid
where DATEPART(year, PickupDate) = @MyYear
group by a.AgencyID 
order by a.AgencyID

それは完全に機能し、次のように応答します

name YearTotal MoAvg 1, 2, 3, 4, 5, 6, 7,8,9,10,11,12
test   13       1     1  1  1  1 1  1  1 1 1 1 1 1 2

各列の単純な合計の合計を次のような列に表示する必要があります

        name YearTotal MoAvg 1, 2, 3, 4, 5, 6, 7,8,9,10,11,12
        total   25       2    2  2  2  2 2  2  2 2 2 2 2 2 3  
        test   13       1     1  1  1  1 1  1  1 1 1 1 1 1 2
        test   12       1     1  1  1  1 1  1  1 1 1 1 1 1 1

それを行う方法はありますか?私はこのパズルを何時間も解こうとしていますが、まだ理解できません

4

1 に答える 1

2

これは通常、プレゼンテーション層でより適切に処理されます。UNIONただし、集計を使用して上記のクエリをサブクエリとして組み合わせ、クエリを使用して同じ結果を得ることができますSUM。SQL Server 2008 を使用しているため、共通のテーブル式も使用できます。

WITH CTE AS (
SELECT
    MAX(a.AgencyName) Name,
    COUNT(PickupID) as YearTotal,
    COUNT(PickupID)/(CASE WHEN YEAR(GETDATE()) = YEAR(PickupDate) THEN MONTH(GETDATE()) ELSE 12 END) as MoAvg,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 1 THEN 1 ELSE 0 end) as Jan,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 2 THEN 1 ELSE 0 end) as Feb,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 3 THEN 1 ELSE 0 end) as Mar,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 4 THEN 1 ELSE 0 end) as Apr, 
    SUM(CASE WHEN DATEPART(month, PickupDate) = 5 THEN 1 ELSE 0 end) as May,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 6 THEN 1 ELSE 0 end) as Jun,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 7 THEN 1 ELSE 0 end) as Jul,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 8 THEN 1 ELSE 0 end) as Aug, 
    SUM(CASE WHEN DATEPART(month, PickupDate) = 9 THEN 1 ELSE 0 end) as Sep,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 10 THEN 1 ELSE 0 end) as Oct,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 11 THEN 1 ELSE 0 end) as Nov,
    SUM(CASE WHEN DATEPART(month, PickupDate) = 12 THEN 1 ELSE 0 end) as Dec 
FROM dbo.Pickup p 
    JOIN Agency a ON p.agencyid = a.agencyid
GROUP BY a.AgencyID, YEAR(PickupDate)
)
SELECT * FROM CTE
UNION
SELECT 'Total',
    SUM(YearTotal) YearTotal,
    SUM(MoAvg) MoAvg,
    SUM(Jan) Jan,
    SUM(Feb) Feb,
    ....
    SUM(Dec) Dec
FROM CTE
于 2013-04-25T01:30:16.977 に答える