欢迎光临散文网 会员登陆 & 注册

SQL面试题:连续登录问题

2023-06-28 17:41 作者:kaisa_03  | 我要投稿

连续登录 减去排序常数结果应是一致的。

with t1 as (select distinct uid,date(login_time)ymd

from t_login where login_time between timestamp '2022-01-01 00:00:00' and timestamp '2022-01-31 23:59:59')

t2 as (select uid,ymd ,

date_sub(ymd,interval row_number() over (partition by uid order by ymd) day )sub_date

from t1)

select uid ,min(ymd),max(ymd),count(*)

from t2 group by uid,sub_date

having count(*) >=5;

(2)连续登录大于等于3天

with t1 as (select distinct uid,date(login_time)ymd

from t_login where login_time between timestamp '2022-01-01 00:00:00' and timestamp '2022-01-31 23:59:59'),

t2 as (

select distinct uid,ymd,

datediff (lag(ymd,2) over (partition by uid order by ymd) )diff

from t1)

select uid,date_sub(ymd,interval 2 day)min_date ,ymd

from t2

where diff >=2;







SQL面试题:连续登录问题的评论 (共 条)

分享到微博请遵守国家法律