对于所有列,我想以1天的间隔合并下表中的信息,计算每个区间中的uuids数量
uuid days_to_signup days_to_doc_submission1
1 1,5 3
2 2,5 5
3 3,2 3,6
4 0,5 4,2
5 200 250
我想要一个这样的表:
time count(days_to_signup) count(days_to_doc_submission1)
0-1 1 0
1-2 1 0
2-3 1 0
3-4 1 2
4-5 0 1
...
200-201 1 0
201-202 0 0
...
250-251 0 1
这里的一个大问题是在没有定义案例的多个子句的情况下这样做。天数可以达到2年(730天)。
但是,我认为知道hw以更一般的方式(例如,不同的间隔)来做这件事。谢谢
这是你想要的吗?
select gs.t,
(select count(*)
from t
where t.days_to_signup >= t and t.days_to_signup < t + 1
) as count_dts,
(select count(*)
from t
where t.days_to_doc_submission1 >= t and t.days_to_doc_submission1 < t + 1
) as count_dds
from generate_series(0, 251, 1) as gs(t)
order by gs.t;
generate_series()
是Postgres中的内置函数,可生成一系列数字或日期。这为您提供了所需的行。实际计数使用相关子查询完成。
有其他方法,但这似乎是最简单的方法。
一种更有效的方法是:
with x as (
select days_to_signup as days, 1 as dts, 0 as dss
from t
union all
select days_to_doc_submission1, 0, 1
from t
)
select gs.t, coalesce(sum(dts), 0) as dts, coalesce(sum(dss), 0) as dss
from generate_series(0, 251, 1) gs(t) left join
x
on x.days >= gs.t and
x.days < gs.t
group by gs.t
order by gs.t;