首页 > 解决方案 > 年月变化百分比

问题描述

我有明确计算按月和年分组的授权的代码。我添加了一个计算字段来显示变化的百分比,但我的问题是我只想逐月获得这个百分比。我的代码计算前每一行的百分比。当拉入 SSRS 时,在 for 列之后显示不正确的值。

Select D.Month
        ,D.Year
        ,count( distinct D.authorization_number) [Admission Events]
        ,CAST(lag(Count(distinct D.authorization_number), 1) over (order by D.month) - Count(distinct D.authorization_number) as FLOAT) / CAST(Count(distinct D.authorization_number)as FLOAT)   [Admission Events Pct]
From #Detail D
Group BY D.Month
        ,D.Year 

在这些结果中,我只想在 SSRS 中显示 2018 年的 pct。

Month   Year    Admission Events    Admission Events Pct
1      2017     5919                 NULL
1      2018     6057                 -0.0227835562159485
2      2017     5302                  0.142399094681252
2      2018     5234                  0.0129919755445166
3      2017     5548                 -0.0565969718817592
3      2018     5389                  0.0295045462980145
4      2017     5128                  0.0508970358814353
4      2018     5503                 -0.0681446483736144
5      2017     5768                 -0.0459431345353675
5      2018     5708                  0.0105115627189909
6      2017     5461                  0.0452298113898553
6      2018     2606                  1.09554873369148

标签: sqlreporting-services

解决方案


这是你想要的吗?

select t.*
from (<your query here>) t
where year = 2018;

您需要一个子查询或 CTE,这样它就where不会干扰lag().


推荐阅读