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

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

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

with t1 as 
(select uid,min(date(in_time))as dt,max(date(in_time)) as dt_max
from tb_user_log 
group by uid )

select case when datediff('2021-11-04',dt) <=6 then '新晋用户'
 when datediff('2021-11-04',dt_max) <=6 then '忠实用户'
 when datediff('2021-11-04',dt_max) >=30 then '流失用户'
else  '沉睡用户' end as user_grade,
round(count(distinct uid)/(select count(distinct uid) from tb_user_log),2) as ratio
from t1 
group by user_grade
order by ratio desc

首先算出每个用户的新增日期和最近活跃日期
然后判断每个用户属于哪个类别
case when的判断是从上而下的,所以顺序可以设计一下;
对于时间差是<7还是<=7 要判断好。

全部评论

相关推荐

哈哈哈哈哈哈哈哈哈哈这个世界太美好了
凉风落木楚山秋:毕业出路老师不管,你盖个章他好交差就完事了,等你盖完毕业了就不关他事情了
点赞 评论 收藏
分享
05-23 20:31
已编辑
武汉大学 Java
内向的柠檬精在研究求...:注意把武大标粗标大 本地你俩不是乱杀
实习进度记录
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-09 12:05
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务