分组变量的百分比变化

kx1ctssn  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(337)

我有四个分组变量 Month , State , County , City . 此外,我还有公制列 sales 它可以是空的,我想计算每个月销售额的百分比变化 City . 我的解决方案将具有相同的分组,但具有 sales 列替换为2019日历年每月的百分比变化。如有任何帮助,我们将不胜感激。

2uluyalo

2uluyalo1#

可以使用窗口函数:

select month, state, city, sales,
       lag(sales) over (partition by state, city order by month) as prev_month,
       (-1 + sales / lag(sales) over (partition by state, city order by month)) as change_ratio
from t;

相关问题