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

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

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

1.标记每条记录出现的时间是7天内还是30天内或是30天外,0,1 7d,30d,da30

2.按照uid sum1步骤做的标记,当每个标记都大于等于1,说明这个uid在这段时间出现过

按照需求:sum(7d) a,sum(30d) b,sum(da30) c

当a>0,b=0,c=0 新晋用户

当a>0,b>0 或a>0,c>0 忠实用户

当a=0,b>0 沉睡用户

当a=0,b=0,c>0 流失用户

3.计算,排序

完整代码:

with t as (

select uid,date(in_time),if(date(in_time) >= date_sub((select max(in_time) from tb_user_log),interval 7 day),1,0) as 7d,

if(date(in_time) < date_sub((select max(in_time) from tb_user_log),interval 7 day) and date(in_time) >= date_sub((select max(in_time) from tb_user_log),interval 30 day),1,0) as 30d,

if(date(in_time) < date_sub((select max(in_time) from tb_user_log),interval 30 day),1,0) as ls

from tb_user_log a

order by uid desc

),

t1 as (

select uid,sum(7d) a,sum(30d) b,sum(ls) c from t group by uid

),

t2 as (

select uid,a,b,c,

case when a>0 and b=0 and c=0 then '新晋用户'

when (a>0 and b>0 ) or (a>0 and c>0) then '忠实用户'

when a=0 and b>0 then '沉睡用户'

when a=0 and b=0 and c>0 then '流失用户' end as lx from t1

)

select lx,

round(count(1)/(select count(distinct uid) from tb_user_log),2) rate

from t2 group by lx

order by rate desc

全部评论

相关推荐

05-30 18:54
武汉商学院 Java
湫湫湫不会java:先投着吧,大概率找不到实习,没实习的时候再加个项目,然后把个人评价和荣誉奖项删了,赶紧成为八股战神吧,没实习没学历,秋招机会估计不多,把握机会。或者说秋招时间去冲实习,春招冲offer,但是压力会比较大
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

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