计算不同年份的总计

时间:2013-03-19 00:59:59

标签: tsql reporting-services ssrs-2008

我想知道如何根据SSRS中设置的年份参数来计算某个计数基数的总计。选择certian年份时,前几年的所有计数将与当前计数相加。在我的存储过程中使用@year参数并且它在SSRS中工作之前,但这次我想尝试对其进行硬编码,然后在SSRS中只有一个年份参数。

以下是我目前正在处理的代码示例:

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作为总计。任何指针都会受到赞赏。

1 个答案:

答案 0 :(得分:2)

基于年份参数的CASE语句求和如下:

SUM(CASE WHEN Year(c_Date) <= @Year THEN MITotal.Count END) AS 'Historical Total'
相关问题