SQL滑动window聚合(不使用window函数)

SQL sliding window aggregation (without using window function)

我正在寻找可以按以下方式在 21 天前汇总数据的查询: 我的 table 有以下列: accountid, 日期, measure

我需要为每个账户计算过去 21 天前的总和(度量)日期。 知道如何在没有 window/analytic 功能的情况下在纯 SQL 中做到这一点吗? (我在不支持分析功能的 BI 产品中编写 SQL)

一种相当低效的方法使用相关子查询。如果您想要前 21 天的每个条目,则:

select t.*,
       (select sum(t2.measure)
        from t t2
        where t2.accountid = t.accountid and
              t2.date > t.date - interval '21' day
       ) as sum21
from t;

并不是说日期函数因数据库而异,因此您的特定数据库可能有另一种减去 21 天的方法。