题解 | #统计活跃间隔对用户分级结果#
统计活跃间隔对用户分级结果
https://www.nowcoder.com/practice/6765b4a4f260455bae513a60b6eed0af
明确思路:
划分依据有两个
1. 活跃间隔,可以等价于最后一次活跃到今天的差距;
2. 是否新晋用户,可以等价于第一次活跃到今天的差距
提及的三个时间为
1. 最后一次活跃,max(out_time) group by uid
2. 第一次活跃,min(in_time) group by uid
3. 今天,max(out_time)
select user_grade, round(count(uid) / any_value(total_cnt),2) as ratio from (select uid, count(uid) over() as total_cnt, case when datediff((max(last_dt) over()), new_dt) <7 then '新晋用户' when datediff((max(last_dt) over()), last_dt) between 7 and 29 then '沉睡用户' when datediff((max(last_dt) over()), last_dt) >= 30 then '流失用户' else '忠实用户' end as user_grade from (select uid, date(min(in_time)) as new_dt, date(max(out_time)) as last_dt from tb_user_log group by uid) as t1 ) as t3 group by user_grade order by ratio desc;