题解 | #统计活跃间隔对用户分级结果#
统计活跃间隔对用户分级结果
https://www.nowcoder.com/practice/6765b4a4f260455bae513a60b6eed0af
with tmp
as(
select
uid
,date(min(in_time)) first_date
,date(max(in_time)) last_date
,(select date(max(in_time)) from tb_user_log) today
from tb_user_log
group by uid
)
select
grade
,round(count(distinct uid) / (select count(distinct uid) from tb_user_log), 2) ratio
from
(
select
uid
,case when datediff(today, first_date) <= 6 then '新晋用户'
when datediff(today, first_date) > 6 and datediff(today, last_date) <= 6 then '忠实用户'
when datediff(today, first_date) > 6 and datediff(today, last_date) >= 30 then '流失用户'
when datediff(today, first_date) > 6 and datediff(today, last_date) > 6 then '沉睡用户'
else '其他'
end grade
from tmp
) t
group by grade
查看3道真题和解析