题解 | #查询连续登陆的用户#
查询连续登陆的用户
https://www.nowcoder.com/practice/9944210610ec417e94140ac09512a3f5
/* 问题:请查询连续登陆不少于3天的新注册用户? 注:登录表为单日随机一次登录数据,该题忽略单日多次登录情况。 要求:输出user_id并升序排序 新注册用户 则 login_tb的user_id必须是register_tb的子集 查询连续登陆不少于3天; */ with a1 as (select l.user_id ,day(log_time) as days ,row_number()over(partition by user_id order by log_time asc) as t_rank from login_tb l where l.user_id in (select user_id from register_tb) )#剔除老用户登录的痕迹 , a1_chazhi as (select * ,days-t_rank as diff from a1 ) ,con_days as ( select user_id, diff, count(days) as con from a1_chazhi group by user_id,diff ) select user_id from con_days where con>=3
求一周期内连续登录日期长度问题
核心就是 根据要求按用户分组,然后进行唯一序号排列,若登录日期是连续的,则其(日期相邻两行差值=1 ) = (唯一序号两行的差值=1),所以 (A+1)-(B+1)=A-B ,连续的时候差值是相同的,而且中间若断开,日期失去连续性,序号保持连续性,则下一波连续日期与序号的差值会变化,成为了同一用户某一周期内断开的连续日期的再分组依据