又刷了一遍SQL题。
user_id(用户ID):VARCHAR。 login_time(用户登录日期):DATE。
SELECT a.user_id
,COUNT(*) AS num
FROM
(
SELECT user_id
,login_time
,LEAD(login_time,1) OVER (PARTITION BY user_id ORDER BY login_time) AS next_login_time
FROM login_info
) AS a
WHERE TIMESTAMPDIFF(DAY, login_time, next_login_time) < 5
GROUP BY user_id;
/* 你还有其他解法吗?可以思考下
推荐阅读
欢迎长按扫码关注「数据管道」
评论