题解 | #没有重复项数字的全排列#

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

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

将问题做以下分解

  1. 分别求用户在7天内,7-30天,和30天以上的登录次数
  2. 根据登录次数来判断用户属性
  3. left join b on 1的语法,将uid的总数关联到原始的查询上
select user_grade,
round(count(1) / max(cnt), 2)
from (
select 
       case when d7 > 0 and d30 = 0 and dm =0 then '新晋用户'
            when d7 > 0 and (d30 > 0 or dm > 0) then '忠实用户'
            when d7 = 0 and d30 > 0 and dm = 0 then '沉睡用户'
            else '流失用户' end user_grade
from 
(
select uid,
max(in_time) as last_active,
count(case when datediff('2021-11-04', in_time) < 7 then 1 else null end) d7,
count(case when datediff('2021-11-04', in_time) >= 7 and datediff(in_time, now()) < 30 then 1 else null end) d30,
count(case when datediff('2021-11-04', in_time) >= 30 then 1 else null end) dm
from tb_user_log
group by 1) t1
) t2
left join (select count(distinct uid) cnt from tb_user_log) t3 
on 1
group by 1 
order by 2 desc 

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-08 13:05
点赞 评论 收藏
分享
05-29 09:02
门头沟学院 Java
点赞 评论 收藏
分享
每晚夜里独自颤抖:你cet6就cet6,cet4就cet4,你写个cet证书等是什么意思。专业技能快赶上项目行数,你做的这2个项目哪里能提现你有这么多技能呢
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 11:30
仁者伍敌:kpi都懒得刷了属于是
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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