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

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

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

先计算各个用户的初次登录时间(first_day),最近登录时间(recent_day),以及总的计算下当前的日期(now)

	SELECT 
        uid,
        (select date(max(out_time)) from tb_user_log) as 'now',
        date(min(in_time)) as 'first_day',
        date(max(out_time)) as 'recent_day'
    from tb_user_log as A
    group by uid

接着按照要求对用户分层

    select 
        uid,
        (case
             when timestampdiff(day,first_day,now) <= 6 then '新晋用户'
             when timestampdiff(day,recent_day,now) <= 6 then '忠实用户'
             when timestampdiff(day,recent_day,now) >= 29 then '流失用户'
             when timestampdiff(day,recent_day,now) < 29 then '沉睡用户'
        end) as 'user_grade'
    from temp

总代码如下:

with temp as (
    SELECT 
        uid,
        (select date(max(out_time)) from tb_user_log) as 'now',
        date(min(in_time)) as 'first_day',
        date(max(out_time)) as 'recent_day'
    from tb_user_log as A
    group by uid
),res as (
    select 
        uid,
        (case
             when timestampdiff(day,first_day,now) <= 6 then '新晋用户'
             when timestampdiff(day,recent_day,now) <= 6 then '忠实用户'
             when timestampdiff(day,recent_day,now) >= 29 then '流失用户'
             when timestampdiff(day,recent_day,now) < 29 then '沉睡用户'
        end) as 'user_grade'
    from temp
)
select user_grade,round(count(*)/(select count(*) from res),2) as 'ratio'
from res
group by user_grade
order by ratio desc
全部评论

相关推荐

双尔:你就写拥有ai开发经历,熟练运用提示词,优化ai,提高ai回答质量
点赞 评论 收藏
分享
在秋招的河老师很爱吃:40岁失业 假如22岁开始工作 18年前 2007年那个时候如何 2015年开始互联网爆发 这个时候有了8年经验 然后黄金10年 你想想这些年挣了多少 这要是进的时候某个大厂 这辈子根本花不完了
你找实习最大的坎坷是什么
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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