请把你create table 的语句及模拟的insert数据贴出来,并用表格说明最后需要的查询效果
目前根据你问题描述,无法知晓你在问什么东西
大概这样,只需要查一次这张表,where 条件里包含有你需要的数据范围,然后在count里加case when 来进行指定条件的统计
with t(user_id,dt) as (
select 1,date'2022-01-01' from dual union all
select 1,date'2022-01-02' from dual union all
select 1,date'2022-01-16' from dual
)
select min(dt),
count(distinct case when dt=date'2022-01-01'+1 then user_id end)/
count(distinct case when dt=date'2022-01-01' then user_id end) b,
count(distinct case when dt=date'2022-01-01'+7 then user_id end)/
count(distinct case when dt=date'2022-01-01' then user_id end) c,
count(distinct case when dt=date'2022-01-01'+15 then user_id end)/
count(distinct case when dt=date'2022-01-01' then user_id end) d,
count(distinct case when dt=date'2022-01-01'+30 then user_id end)/
count(distinct case when dt=date'2022-01-01' then user_id end) e from t where dt
between date'2022-01-01' and date'2022-01-01'+30
以上是oracle中的语法,其他数据库可能要处理下日期表示和日期计算,应该能看懂吧?