对于每个月,计算间隔的条目

for each month, count entries with interval

我很难创建包含每月持续订阅总和的统计数据

我有 table 个订阅

id | created_at          | cancelled_at
----------------------------------------
 1 | 2020-12-29 13:56:12 | null
 2 | 2021-02-15 01:06:25 | 2021-04-21 19:35:31
 3 | 2021-03-22 02:42:19 | null
 4 | 2021-04-21 19:35:31 | null

统计数据应如下所示:

month   | count
---------------
12/2020 | 1     -- #1
01/2021 | 1     -- #1
02/2021 | 2     -- #1 + #2
03/2021 | 3     -- #1 + #2 + #3
04/2021 | 3     -- #1 + #3 + #4, not #2 since it ends that month
05/2021 | 3     -- #1 + #3 + #4

到目前为止,我能够列出我需要统计数据的所有月份:

select generate_series(min, max, '1 month') as "month"
from (
    select date_trunc('month', min(created_at)) as min,
    now() as max
    from subscriptions
) months;

并获得特定月份的正确订阅数量

select sum(
    case 
        when
            make_date(2021, 04, 1) >= date_trunc('month', created_at) 
            and make_date(2021, 04, 1); < date_trunc('month', coalesce(cancelled_at, now() + interval '1 month'))
        then 1
        else 0
    end
) as total
from subscriptions
-- returns 3

但我正在努力将它们组合在一起... OVER(我没有经验)对我有用吗?我找到了 Count cumulative total in Postgresql 但它是不同的情况(日期是固定的)......或者是以某种方式使用 FOR 函数的正确方法?

您可以使用 generate_series() 生成月份,然后使用相关子查询来计算活跃度:

select yyyymm,
       (select count(*)
        from subscriptions s
        where s.created_at < gs.yyyymm + interval '1 month' and
              (s.cancelled_at > gs.yyyymm + interval '1 month' or s.cancelled_at is null)
       ) as count
from generate_series('2020-12-01'::date, '2021-05-01'::date, interval '1 month'
                    ) gs(yyyymm);