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

各用户活跃分层人数统计

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

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

全部评论

相关推荐

去B座二楼砸水泥地:不过也可以理解,这种应该没参加过秋招
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
10-12 10:48
已编辑
秋招之苟:邻居家老哥19届双2硕大厂开发offer拿遍了,前几天向他请教秋招,他给我看他当年的简历,0实习实验室项目技术栈跟开发基本不沾边😂,我跟他说这个放在现在中厂简历都过不了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务