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

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

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




select c.tag
        ,round(count(c.uid)/(select count(distinct uid) from tb_user_log),2) as ratio
from
    (
      select a.uid
              ,case when timestampdiff(day,a.last_out,a.today_time) <=6 and b.last_out_times >1 then '忠实用户'
              when timestampdiff(day,a.last_out,a.today_time) <=6 and b.last_out_times =1 then '新晋用户'
              when timestampdiff(day,a.last_out,a.today_time) >6 and timestampdiff(day,a.last_out,a.today_time) <=29 then '沉睡用户'
              else '流失用户' end as tag
      from
          (
            select uid
                    ,(select max(out_time)  from tb_user_log) as today_time
                    ,max(out_time) last_out
                    ,max(date(out_time)) dt
            from tb_user_log
            group by uid) a,  #a表:用户最近登陆时间和今天的间隔
            (
              select uid
                      ,date(out_time) as dt
                      ,row_number() over(partition by uid order by date(out_time) asc) as last_out_times
              from tb_user_log) b  #b表:用户最近登陆时间是否为首次登陆
      where a.uid=b.uid and a.dt = b.dt) c #c表:用户最近登陆时间和首次登陆判定
group by c.tag
order by ratio desc;

全部评论

相关推荐

昨天 17:01
门头沟学院 Java
点赞 评论 收藏
分享
03-25 19:00
东北大学 Java
程序员牛肉:太好了,是聊天记录。不得不信了。 当个乐子看就好,不要散播焦虑
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务