题解 | #统计活跃间隔对用户分级结果#
统计活跃间隔对用户分级结果
https://www.nowcoder.com/practice/6765b4a4f260455bae513a60b6eed0af
select case when timestampdiff(day, last_date, max_date) >= 30 then '流失用户' when timestampdiff(day, last_date, max_date) >= 7 then '沉睡用户' when timestampdiff(day, first_date, max_date) < 7 then '新晋用户' else '忠实用户' end as user_grade, round(count(*) / max(total), 2) as ratio from (select uid, min(date(in_time)) as first_date, max(date(out_time)) as last_date from tb_user_log group by uid) as t1 left join (select max(date(out_time)) as max_date, count(distinct uid) as total from tb_user_log ) as t2 on 1 group by user_grade order by ratio desc