题解 | #各用户活跃分层人数统计#

各用户活跃分层人数统计

https://www.nowcoder.com/practice/d76cc30d1af9465abeff3ad663d7e60e

with cte as (
select uid,min(login_date)       as reg_date,     #注册时间
           max(login_date)       as rec_date,     #最近活跃时间
           max(max(login_date))over() as cur_date      #现在时间
from user_login_tb
group by uid    ),
cte1 as (
select uid,login_date as sec_date,                #次活跃时间
           row_number()over(partition by uid order by login_date desc) as rn
from user_login_tb ),
cte2 as (
select a.uid,reg_date,rec_date,cur_date,sec_date
from cte a left join cte1 b on a.uid=b.uid and b.rn=2 ),
cte3 as (
select uid, case when datediff(cur_date,reg_date)<=6  then '新增用户'
                 when datediff(cur_date,rec_date)>=30 then '流失用户'
                 when datediff(cur_date,rec_date)>6   then '沉默用户'
                 when rec_date=cur_date and datediff(cur_date,sec_date)>=30 then '回流用户'
            else '忠实用户' end as user_grade
from cte2 )
select user_grade,count(*) as num,
                  round(count(*)/sum(count(*))over(),2) as ratio
from cte3
group by user_grade
order by ratio desc

逻辑上感觉不是很严谨,不知道什么样的公司会这样给客户分群

全部评论

相关推荐

小红书 后端选手 n*16*1.18+签字费期权
点赞 评论 收藏
分享
头像
10-09 19:35
门头沟学院 Java
洛必不可达:java的竞争激烈程度是其他任何岗位的10到20倍
点赞 评论 收藏
分享
我已成为0offer的糕手:别惯着,胆子都是练出来的,这里认怂了,那以后被裁应届被拖工资还敢抗争?
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务