题目
现有用户登录日志表 t_login_log,包含用户ID(user_id),登录日期(login_date)。数据已经按照用户日期去重,请查出连续登录超过4天的用户ID。
样例数据
样例输出
建表语句
CREATE TABLE t_login_log (user_id VARCHAR(255) COMMENT '用户ID',login_date DATE COMMENT '登录日期'
);
insert into t_login_log values
('0001','20220101'),
('0001','20220102'),
('0001','20220103'),
('0001','20220104'),
('0001','20220105'),
('0001','20220107'),
('0001','20220108'),
('0001','20220109'),
('0002','20220101'),
('0002','20220102'),
('0002','20220103'),
('0002','20220107'),
('0002','20220108'),
('0003','20220107'),
('0003','20220108'),
('0003','20220109');
题解
select
user_id
from
(select
*,row_number() over(partition by user_id order by login_date) rn
from t_login_log)t
group by user_id,date_sub(login_date,interval rn day)
having count(*)>4;