题解 | SQLW11 查询连续登陆的用户

WITH t1 AS (
    SELECT user_id,
           DATE(log_time) AS log_date
    FROM login_tb
    WHERE user_id in
    (SELECT user_id FROM register_tb)
    GROUP BY user_id, DATE(log_time)
),
t2 AS (
    SELECT user_id,
           log_date,
           RANK() over (PARTITION BY user_id
               ORDER BY log_date) AS ranking
    FROM t1
),
t3 AS (
    SELECT user_id,
           date_add(log_date,INTERVAL -ranking DAY) AS after_date
    FROM t2
),
t4 AS (
    SELECT user_id,
           after_date,
           COUNT(after_date) AS cont
    FROM t3
    GROUP BY user_id, after_date
)
SELECT user_id
       FROM t4
WHERE cont >= 3
ORDER BY user_id ASC;

# 常规的连续登录问题!

全部评论

相关推荐

2024-11-18 15:57
门头沟学院 Java
最终归宿是测开:这个重邮的大佬在重邮很有名的,他就喜欢打92的脸,越有人质疑他,他越觉得爽😂
点赞 评论 收藏
分享
qz鹿:*** 祝他毕业就失业
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务