sql - 年月变化百分比
问题描述
我有明确计算按月和年分组的授权的代码。我添加了一个计算字段来显示变化的百分比,但我的问题是我只想逐月获得这个百分比。我的代码计算前每一行的百分比。当拉入 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
解决方案
这是你想要的吗?
select t.*
from (<your query here>) t
where year = 2018;
您需要一个子查询或 CTE,这样它就where
不会干扰lag()
.
推荐阅读
- c++ - 在我的凯撒密码项目中未定义对“Caesar::Caesar(int)”的引用
- chart.js - Chart.js 货币工具提示
- sql - 在 Athena 中将两列映射为一列
- makefile - Makefile错误:没有规则来制作“build”需要的“bin/functions.o”
- python - 我在 python 中使用 phonenumbers 模块试图找出一个数字载体。输入数字后什么都不打印
- linux - 将标准输出和错误重定向到同一文件的 Bash shell 中的事件顺序
- python - “OSError:[Errno 49] 无法分配请求的地址”OS X python Socket
- python - 为什么以下 python 代码只在 for 循环内删除一次元素?
- python - 为什么这个 TensorFlow Serving gRPC 调用会挂起?
- excel - 将数据从 MS EXCEL 导出到 MS POWERPOINT 时出错