题解 | #统计活跃间隔对用户分级结果#

统计活跃间隔对用户分级结果

https://www.nowcoder.com/practice/6765b4a4f260455bae513a60b6eed0af

参考了其他大佬的解法  但是那个max(max)over()实在不懂什么意思 就写的比较繁琐
代码如下:
with t1 as (select case
       when datediff(now_dt,min_dt) <= 6 then '新晋用户'
       when datediff(now_dt,max_dt) <= 6 and datediff(now_dt,min_dt) > 6 then '忠实用户'
       when datediff(now_dt,max_dt) between 7 and 29 then '沉睡用户'
       else '流失用户'
       end as user_grade
from (
select uid,(select max(date(out_time)) from tb_user_log) as now_dt,
       max(date(out_time)) max_dt,min(date(in_time)) min_dt
from tb_user_log
group by uid) a)
select user_grade,round(num/cnt,2) as ratio
from (
select user_grade,(select count(1) from t1) as cnt,count(user_grade) num
from t1
group by user_grade) b
group by user_grade
order by ratio desc
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务