经典sql

一、连续登录问题

问题:1)、每个用户连续登录最大天数

2)、连续登录大于三天的用户数

分析:本质都是计算用户连续登录天数

方案一:利用排序窗口

sql 复制代码
select a.user_id
      ,a.date_rslt
      ,count(1) as cnt
from (
        select    
            t.user_id
            ,t.login_time
            ,date_sub(login_time, num) as date_rslt
         from (
                select 
                    user_id
                    ,login_time
                    ,row_number() over(partition by user_id order by login_time) as num
                from login_log
         ) t
      ) a
group by a.user_id,a.date_rslt

方案二、增量加全量

连续访问天数v_days(最新flag值为1,则v_days累加,否则为0)

历史最大访问天数max_days (从max_days、v_days中取最大值)

sql 复制代码
select 
    coaleasce(h.user_id,i.user_id) as user_id,
    if(i.user_id is not null,v_days+1,0) as v_days,
    greatest(max_days,if(i.user_id is not null,v_days+1,0)) as max_days
from
history_ds h
full join 
log_time i

扩展1:连续登录,中间间隔1天也算

sql 复制代码
select user_id
       ,group_id
       ,(datediff(max(login_date),min(login_date))+1) as continuous_login_days
  from (
      select 
            user_id
            ,login_date
            ,sum(if(date_diff>1,1,0)) over(partition by user_id order by login_date rows between unboundedpreceding and current row) as group_id
      from (
        select 
            user_id
            ,login_date
            ,datediff(login_date,last_login_date) as date_diff
        from (
            select 
                user_id
                ,login_date
                ,lag(login_date,1,'1970-01-01') over(partition by user_id order by login_date) as last_login_date
            from test_login
        )t1
    )t2
)t3
group by user_id
       ,group_id;

扩展2:断点排序

连续日期的数据对应的值发生变化,重新排序

sql 复制代码
select  
  a,
  b,
  row_number() over( partition by b,a_diff order by a) as c
from 
(
  select  
    a,
    b,
    a-num as a_diff
  from 
  (
   select 
     a,
     b,
     row_number() over( partition by b order by  a ) as num
   from t1 
  )tmp1
)tmp2
order by a; 
相关推荐
渣渣盟2 小时前
Flink Table API与SQL流数据处理实战
大数据·sql·flink·scala
疯狂成瘾者8 小时前
上传到 GitHub 的步骤总结
大数据·elasticsearch·github
七夜zippoe12 小时前
OpenClaw 接入 WhatsApp:消息推送实战
大数据·人工智能·microsoft·whatsapp·openclaw
RFID科技的魅力12 小时前
从开箱到实战:CP300R触屏RFID打印机全场景使用测评
大数据·人工智能·物联网·rfid
Rick199313 小时前
慢SQL优化
数据库·python·sql
Forrit14 小时前
Agent长期运行(Long-Running Tasks)实现方案与核心挑战
大数据·人工智能·深度学习
2601_9553631515 小时前
技术赋能B端拓客:号码核验的行业困局与破局路径氪迹科技法人股东筛选系统,阶梯式价格
大数据·人工智能
财经资讯数据_灵砚智能16 小时前
全球财经资讯日报(夜间-次晨)2026年3月28日
大数据·人工智能·python·语言模型·ai编程