应该显示总数的SQL程序

时间:2013-04-25 00:35:45

标签: sql sql-server-2008 select sum procedure

嗨我有工作的程序,看起来像

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

任何想法怎么做?  我试图解决这个难题几个小时仍然无法弄明白

1 个答案:

答案 0 :(得分: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