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

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

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

###  解题思路,首先计算每个用户的最小登录时间in_time(新增用户时间注册时间),和每个用户的最大登出时间out_time ,以及当前表格的最大登出时间out_time ,分别按照 这三个时间的关系将用户划分为 忠实用户,新晋用户,沉睡用户,流失用户;最后一步,统计每个类别用户的用户数,并使用sum(count(uid)) over() 窗口函数统计所有分组的所有用户数。


     select B.user_grade,
            #count(uid) uCnt,
            #sum(count(uid)) over(order by B.user_grade) total1,
            #sum(count(uid)) over() total,
            round(count(uid)/(sum(count(uid)) over()),2) ratio 
       from (
     select uid,
            (case when datediff(nowdt,mindt)>=7 and datediff(nowdt,maxdt)<=6 then 
               '忠实用户' 
                  when datediff(nowdt,mindt)<=6 and datediff(nowdt,maxdt)<=6 then 
                '新晋用户' 
                  when datediff(nowdt,maxdt)>=7 and datediff(nowdt,maxdt)<30 then 
                '沉睡用户' 
                  when datediff(nowdt,maxdt)>=30 then 
                '流失用户' end ) user_grade 
        from (
     select uid,
            min(in_time) mindt, #每个用户的最早登录系统的时间
            max(out_time) maxdt, #每个用户距离今天最近登录系统的时间
            (select max(out_time) from tb_user_log) nowdt # 数据中所有日期的最大值
       from tb_user_log
       group by uid ) A 
      group by uid 
      ) B 
      group by B.user_grade 
      order by ratio desc ;

全部评论

相关推荐

09-12 11:55
已编辑
湖南工商大学 Java
那一天的Java_J...:这种一堆问题的,别去
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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