嗨,我有一个有效的程序,看起来像
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
任何想法如何做到这一点?我试图解决这个难题几个小时仍然无法弄清楚