题解 | #统计活跃间隔对用户分级结果#
统计活跃间隔对用户分级结果
https://www.nowcoder.com/practice/6765b4a4f260455bae513a60b6eed0af
with t1 as (select DISTINCT uid,max(date(in_time)) as in_dt,max(date(out_time)) as out_dt from tb_user_log group by uid), t2 as ( select uid,min(date(in_time)) as early_date from tb_user_log group by uid), t3 as (select max(date(out_time)) as max_date from tb_user_log), tmp as ( select avg(case when DATEDIFF(t3.max_date,t1.out_dt)<=6 and t2.early_date<t1.out_dt then 1 else 0 end ) as a, avg(case when DATEDIFF(t3.max_date,t1.out_dt)<=6 and t2.early_date>=t1.out_dt then 1 else 0 end ) as b, avg(case when DATEDIFF(t3.max_date,t1.out_dt)>6 and DATEDIFF(t3.max_date,t1.in_dt)<30 then 1 else 0 end ) as c, avg(case when DATEDIFF(t3.max_date,t1.out_dt)>=30 then 1 else 0 end ) as d from t1 left join t2 on t1.uid=t2.uid left join t3 on 1 ) select '忠实用户',round(a,2) from tmp union all select '新晋用户',round(b,2) from tmp union all select '沉睡用户',round(c,2) from tmp union all select '流失用户',round(d,2) from tmp;