0

我想知道如何根据 SSRS 中设置的年份参数计算某个计数的总计。When a certian year is selected the previous counts from all the previous years would add up with the current count. 在我在存储过程中使用@year 参数并且它在 SSRS 中工作之前,但这次我想尝试对其进行硬编码,然后在 SSRS 中只有一个 year 参数。

这是我目前正在使用的代码示例:

select
sum(MITotal.Count) as 'Yearly Count',
year(c_date) as 'year'

from 
(select 
    count(*) as Count,
    pol1.C_STATE,
    month(pol1.c_Date) as Month,
    year(pol1.c_ate) as Year,
    left(datename(mm, C_Date), 3) + ' ' + cast(year(C_Date) as char(4)) as MonthYear
    from contract pol1
    where 
        pol1.C_STATE='wa' 
    group by pol1.C_STATE,month(pol1.c_Date),year(pol1.c_Date), left(datename(mm, C_Date), 3) + ' ' + cast(year(C_Date) as char(4))) MITotal
    group by MITotal.Year

这将返回:

2   2009
5   2010
6   2011

我将如何添加到代码中,以便无论何时,我选择说 2010 作为参数年份,它会添加 5+2 或 2011,它会添加 5+6+2 作为总计。任何指针将不胜感激。

4

1 回答 1

2

根据 year 参数对 CASE 语句求和,如下所示:

SUM(CASE WHEN Year(c_Date) <= @Year THEN MITotal.Count END) AS 'Historical Total'
于 2013-03-19T01:59:09.387 回答