PostgreSQL中字段数据后数周的聚合计数

PostgreSQL中字段数据后数周的聚合计数,postgresql,aggregate,Postgresql,Aggregate,我有一个查询返回如下内容: 注册日期-用户注册日期; 动作\某类动作的日期 | 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

我有一个查询返回如下内容: 注册日期-用户注册日期; 动作\某类动作的日期

| 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 | 我正在尝试实现一个查询,该查询将返回如下内容: 注册后的周数-在本例中限制为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 | 您可以使用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和所有可能的用户_id 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;

非常感谢。你真棒!