sql 时间区间怎么写 有无能人

一个留存sql 次日 7日,15日,30日留存
建了5个表abcde。 分别用bcda/a表得出留存,但是时间该怎么写呢 一天天跑太累了

请把你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

img

以上是oracle中的语法,其他数据库可能要处理下日期表示和日期计算,应该能看懂吧?