with tb1 as( select uid, date(in_time) dt from tb_user_log union all select uid, date(out_time) dt from tb_user_log) select tb3.dt, round(count(tb4.uid)/count(tb3.uid),2) from (select distinct uid, dt from( select uid, dt, min(dt)over(partition by uid) min_dt from tb1) tb2 where dt=min_dt) tb3 left join (select distinct uid, dt from tb1) tb4 on tb3.uid=tb4.uid and datediff(tb4.dt,tb3.dt)=1 where date_format(tb3.dt,'%Y%m')='202111' group by tb3.dt order by tb3.dt #思路: #①因可能跨天活跃,所以将in_time和out_time先用union联立,形成表tb1备用; #②找出新用户表,即min_dt=dt,形成tb3; #③找出新用户次日活跃的数据,即将tb2与用户表左连接,加入连接条件:日期相差1天; #④根据日期分组,计算即可。