当前位置: 代码网 > it编程>数据库>MsSqlserver > SQL统计连续登陆3天用户的实现示例

SQL统计连续登陆3天用户的实现示例

2024年06月13日 MsSqlserver 我要评论
1. 数据准备-- 数据准备with user_active_info as (select * from ( values ('10001' , '2023-02-01'),('10001'

1. 数据准备

-- 数据准备
with user_active_info as (
select * from (
    values ('10001' , '2023-02-01'),('10001' , '2023-02-03')
          ,('10001' , '2023-02-04'),('10001' , '2023-02-05')
          ,('10002' , '2023-02-02'),('10002' , '2023-02-03')
          ,('10002' , '2023-02-04'),('10002' , '2023-02-05')
          ,('10002' , '2023-02-07'),('10003' , '2023-02-02')
          ,('10003' , '2023-02-03'),('10003' , '2023-02-04')
          ,('10003' , '2023-02-05'),('10003' , '2023-02-06')
          ,('10003' , '2023-02-07'),('10003' , '2023-02-08')
          ,('10004' , '2023-02-03'),('10004' , '2023-02-04')
          ,('10004' , '2023-02-06'),('10004' , '2023-02-07')
          ,('10004' , '2023-02-08'),('10004' , '2023-02-08') 
    	  ,('10005' , '2023-02-02'),('10005' , '2023-02-05') 
) as user_active_info(user_id, active_date) 
)

2. 方法一: 差值计算

-- 1. 对用户数据进行分组,按照活跃日期进行排序(去重:防止有一天有多次活跃记录)
select 
      user_id
    , active_date
    , row_number() over(partition by user_id order by active_date) as rn 
from user_active_info
group by user_id , active_date
; 
user_idactive_datern
100012023-02-011
100012023-02-032
100012023-02-043
100012023-02-054
100022023-02-021
100022023-02-032
100022023-02-043
100022023-02-054
100022023-02-075
-- 2. 使用活跃日期和排序rn进行差值计算,得到的日期如果是相等的,就说明活跃日期是连续的
select 
      user_id
    , active_date
    , rn 
    , date_sub(active_date,rn) as sub_date
from (
    select 
          user_id
        , active_date
        , row_number() over(partition by user_id order by active_date) as rn 
    from user_active_info 
    group by user_id , active_date
    ) a
; 
user_idactive_daternsub_date
100012023-02-0112023-01-31
100012023-02-0322023-02-01
100012023-02-0432023-02-01
100012023-02-0542023-02-01
100022023-02-0212023-02-01
100022023-02-0322023-02-01
100022023-02-0432023-02-01
100022023-02-0542023-02-01
100022023-02-0752023-02-02
-- 3. 按照user_id和sub_date 进行分组求和,筛选出连续登陆天数大于3天的用户
select 
      user_id
    , min(active_date) as begin_date
    , max(active_date) as end_date
    , count (1) as login_duration
from (
    select 
          user_id
        , active_date
        , rn 
        , date_sub(active_date,rn) as sub_date
    from (
        select 
              user_id
            , active_date
            , row_number() over(partition by user_id order by active_date) as rn 
        from user_active_info 
        group by user_id , active_date
    ) a
) b
group by user_id , sub_date
having login_duration >= 3
; 
user_idbegin_dateend_datelogin_duration
100012023-02-032023-02-053
100022023-02-022023-02-054
100032023-02-022023-02-087
100042023-02-062023-02-083

3. 方法二: lead或lag函数

-- 1. 将active_date 上抬2行,不存在默认为'0'(计算连续活跃3天以上的, 上抬2行,n天上抬n-1行)(去重:防止有一天有多次活跃记录)
select 
      user_id
    , active_date
    , lead(active_date , 2 , 0) over(partition by user_id order by active_date) as lead_active_date 
from user_active_info
group by user_id , active_date
user_idactive_datelead_active_date
100012023-02-012023-02-04
100012023-02-032023-02-05
100012023-02-040
100012023-02-050
100022023-02-022023-02-04
100022023-02-032023-02-05
100022023-02-042023-02-07
100022023-02-050
100022023-02-070
-- 2. 过滤筛选出, lead_active_date 与 active_date 差值为2的, 差值2 -> 连续活跃了3天
select 
      user_id , active_date , lead_active_date
from (
    select 
          user_id
        , active_date
        , lead(active_date , 2 , 0) over(partition by user_id order by active_date) as lead_active_date
    from user_active_info
    group by user_id , active_date
) a 
where  lead_active_date != '0'
    and datediff(lead_active_date , active_date) = 2
user_idactive_datelead_active_date
100012023-02-032023-02-05
100022023-02-022023-02-04
100022023-02-032023-02-05
-- 3. user_id 去重, 得到连续活跃天数>=3天的用户
select 
      user_id
from (
    select 
          user_id , active_date , lead_active_date
    from (
        select 
              user_id
            , active_date
            , lead(active_date , 2 , 0) over(partition by user_id order by active_date) as lead_active_date 
        from user_active_info
        group by user_id , active_date
    ) a 
    where  lead_active_date != '0'
        and datediff(lead_active_date , active_date) = 2
) b
group by user_id
user_id
10001
10002
10003
10004

到此这篇关于sql统计连续登陆3天用户的实现示例的文章就介绍到这了,更多相关sql统计连续登陆3天用户内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com