首页 > 解决方案 > SAS SQL - 如何获得过去 X 个月的累积总和

问题描述

我有一张这样的桌子:

表原样

我想根据 date_ini 和今天计算过去 x 个月中每个 cust_id 列“total”的累积总和,所以我的输出是:

期望的输出

你能帮我在 sas / proc sql 中做这个吗?

谢谢!

标签: sqlsasproc-sqlenterprise-guide

解决方案


看起来您想使用 CASE 来决定 TOTAL 是否有助于您的新总和。

因此,假设您的参考日期是当前日期,您可能会使用这样的日期。

create table want as
  select cust_id
       , sum(case
             when (date_ini >= intnx('month',today(),-1,'b') then total
             else 0 end) as total_last_month
       , sum(case
             when (date_ini >= intnx('month',today(),-36,'b') then total
             else 0 end) as total_last_36months
  from have
  group by cust_id
;

但我不确定我是否会称这些累积总和。


推荐阅读