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

它是完美的 lu 并且响应看起来像

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 回答