SSRS-计算变化年份和年数的百分比变化

时间:2018-12-27 00:27:47

标签: reporting-services ssrs-2012

希望我能清楚地解释这一点!我们的报告允许用户选择不同数量的学年。它们可以是连续的,也可以不是连续的。在这种情况下,假设用户选择2017年,2015年,2013年。报告显示如下:

       AcYear     TotCredits     %Change
       2013       251            0.00
       2015       255            0.00
       2017       1102           0.00

因为提供数据源的SQL假设前一年是前一年。因此,即使我们未选择该信息,也将2015与2014进行了比较,这就是为什么将其设为0的原因。即使用户选择了2015,也将将2017与2016进行了比较。因此,无论是在初始SQL还是在报表中表达式,无论选择了多少个学分,我将如何获得最近一个所选学年的学分,然后根据该学分计算百分比变化?

(如果用户连续选择2017年和2016年,则数据如下:)

        AcYear     TotCredits     %Change
        2016       458             0.00%
        2017       19            -95.85%

这是获得“上一年”年份的自我加入:

from
    cte
    left join cte as prev1
        on cte.academic_year - 1 = prev1.academic_year
        and cte.subject          = prev1.subject

感谢任何想法!

2 个答案:

答案 0 :(得分:0)

您可以尝试使用先前的功能来查看其是否起作用。您需要确保对Year进行了排序。下面是设计

select * from (select 2012  as year, 12  as amount
union all
select 2013  as year, 5  as amount
union all
select 2014  as year, 6  as amount
union all
select 2015  as year, 4  as amount
union all

select 2016  as year, 24  as amount)tt
where year in (@t)

enter image description here

表达:

=iif(Fields!year.Value-previous(Fields!year.Value)=1,(Fields!amount.Value-previous(Fields!amount.Value))/iif(IsNothing(previous(Fields!amount.Value) ), 0,previous(Fields!amount.Value)),0)

然后您将得到如下结果

enter image description here

佐伊

答案 1 :(得分:0)

令人惊讶的简单/优雅的修复程序,花了我很长时间才找到它。原始导入位:

SELECT
    cte.academic_year
  , cte.subject
  , cte.subject_desc
  , cte.credits  as cur_credits
  , prev.credits as prev_credits
FROM
  cte
  LEFT JOIN cte as prev
    on cte.academic_year - 1 = prev.academic_year
    and cte.subject = prev.subject

新的改进代码:

SELECT
    t.academic_year
  , t.subject
  , t.subject_desc
  , t.credits  as cur_credits
  , prev_credits = prev.credits

FROM
    cte t
    OUTER APPLY
        (SELECT top 1 credits from 
         cte 
         WHERE t.academic_year > academic_year
           and t.subject = subject
         ORDER BY academic_year desc) prev

使用LAG会非常好,但是,我们使用的是SQL2008。