在 PostgreSQL 中的字段数据后几周汇总计数

Aggregate count by several weeks after field data in PostgreSQL

我有一个查询 returns 类似这样的问题:
registered_at - 用户注册日期;
action_at - 某种行为的日期。

|       registered_at | user_id |           action_at |
-------------------------------------------------------
| 2015-05-01 12:00:00 |       1 | 2015-05-04 12:00:00 |
| 2015-05-01 12:00:00 |       1 | 2015-05-10 12:00:00 |
| 2015-05-01 12:00:00 |       1 | 2015-05-16 12:00:00 |
| 2015-04-01 12:00:00 |       2 | 2015-04-04 12:00:00 |
| 2015-04-01 12:00:00 |       2 | 2015-04-05 12:00:00 |
| 2015-04-01 12:00:00 |       2 | 2015-04-10 12:00:00 |
| 2015-04-01 12:00:00 |       2 | 2015-04-30 12:00:00 |

我正在尝试实现 returns 类似的查询:
weeks_after_registration - 在本例中限制为 3,在实际任务中将限制为 6。

| user_id |  weeks_after_registration | action_counts |
-------------------------------------------------------
|       1 |                         1 |             1 |
|       1 |                         2 |             1 |
|       1 |                         3 |             1 |
|       2 |                         1 |             2 |
|       2 |                         2 |             1 |
|       2 |                         3 |             0 |

您可以使用 extract(days from (action_at - registered_at) / 7)+1 获取周数。然后计算按周数分组的操作数。

  select user_id, wk, count(*) actions
  from (select user_id, extract(days from (action_at - registered_at) / 7)+1 wk from Table1) a
  where wk <= 3
  group by user_id, wk

如果您必须在结果中显示 action_counts = 0 的行,那么您需要连接所有可能的周数 (1, 2, 3) 和所有可能的 user_ids (1 , 2) 喜欢:

select b.user_id, a.wk, coalesce(c.actions, 0) actions
from (select * from generate_series(1, 3) wk) a
join (select distinct user_id from Table1) b on true
left join (
  select user_id, wk, count(*) actions
  from (select user_id, extract(days from (action_at - registered_at) / 7)+1 wk from Table1) a
  where wk <= 3
  group by user_id, wk
) c on a.wk = c.wk and b.user_id = c.user_id
order by b.user_id, a.wk;

fiddle