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

 更新时间:2024年05月24日 09:55:35   作者:Taerge0110  
最近有个需求,求连续登陆的这一批用户,本文就来介绍一下SQL统计连续登陆3天用户的实现示例,具有一定的参考价值,感兴趣的可以了解一下

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天用户内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

最新评论