SQL连续登录问题(详细案例分析)

如果要统计用户活跃度,那就涉及连续登录问题,接下来将举一个简单的例子来详细说明这个问题:

一、创建一些模拟数据

一些测试数据如下:

deviceid1,2022-10-26,2022-10-26,2022-11-01

deviceid1,2022-10-26,2022-11-03,2022-11-05

deviceid2,2022-10-27,2022-10-27,2022-11-08

deviceid2,2022-10-27,2022-11-10,9999-12-31

deviceid3,2022-10-27,2022-10-27,2022-11-01

deviceid3,2022-10-27,2022-11-04,2022-11-11

deviceid3,2022-10-27,2022-11-14,9999-12-31

deviceid4,2022-10-01,2022-10-01,2022-10-11

deviceid4,2022-10-01,2022-10-13,2022-11-01

deviceid4,2022-10-01,2022-11-03,2022-11-05

sql 复制代码
-- 1.创建用户活跃区间表
create table tmp.app_user_active_range(
  deviceid      string,     -- 设备编号
  first_login   string,     -- 首访日期
  start_dt      string,     -- 用户活跃区间起始时间
  end_dt        string      -- 用户活跃区间结束时间
)
partitioned by(dt string)
row format delimited
fields terminated by ',';

-- 2、模拟用户活跃数据,将上述数据保存到服务器/root/range.txt路径下 然后load到tmp.app_user_active_range表,

load data local inpath '/root/range.txt' overwrite into table  tmp.app_user_active_range partition(dt='2022-11-23');

二、一些统计用户活跃度的问题

下面是三个简单的指标:

1、最近一个月内,每个用户的最大连续活跃天数

2、最近一个月内,连续活跃[1-10)天的人数,[10-20)天的人数,[20+ 天的人数

3、最近一个月内,沉默天数超过3天的有多少人,超过5天有多少人

三、 指标开发详细解决步骤

1、最近一个月内,每个用户的最大连续活跃天数

sql 复制代码
select * from tmp.app_user_active_range;
with t as (
    select *,
       datediff(`if`(end_dt=='9999-12-31',dt,end_dt),`if`(start_dt<date_sub(dt,30),date_sub(dt,30),start_dt))+1 active_days
       from tmp.app_user_active_range where end_dt>=date_sub(dt,30)
    union
    select *,0 from tmp.app_user_active_range where end_dt<date_sub(dt,30)
)select deviceid,max(active_days) max_days from t group by deviceid;

2、最近一个月内,连续活跃[1-10)天的人数,[10-20)天的人数,[20+ 天的人数

sql 复制代码
-- 需求2:最近一个月内,连续活跃[1-10)天的人数,[10-20)天的人数,[20+ 天的人数
with t as (
    select *,
       datediff(`if`(end_dt=='9999-12-31',dt,end_dt),`if`(start_dt<date_sub(dt,30),date_sub(dt,30),start_dt))+1 active_days
       from tmp.app_user_active_range where end_dt>=date_sub(dt,30)
    union
    select *,0 from tmp.app_user_active_range where end_dt<date_sub(dt,30)
),t1 as (
    select deviceid,
        case when active_days between 1 and 9 then '[1-10)'
             when active_days between 10 and 19 then '[10-20)'
             when active_days>=20 then '[20+'
             else '0'
        end as qujian
        from t
)select qujian,count(distinct deviceid) people_num from t1 group by qujian;

3、最近一个月内,沉默天数超过3天的有多少人,超过5天有多少人

sql 复制代码
-- 最近一个月内,沉默天数超过3天的有多少人,超过5天有多少人

-- 最终结果
with  z as (
    -- 前半段沉默时间和中间沉默时间
    with t as (
   select *,
       datediff(start_dt,lag(end_dt,1,`if`(first_login<date_sub(dt,30),date_sub(dt,30),first_login)) over(partition by deviceid order by start_dt))-1 chenmodays
       from tmp.app_user_active_range where end_dt>=date_sub(dt,30)
)select deviceid,chenmodays from t where chenmodays>0
union all
-- 后半段沉默时间
select deviceid,datediff(dt,end_dt)-1 chenmodays from (
    select *,max(end_dt) over(partition by deviceid) max_chenmo from tmp.app_user_active_range where end_dt>=date_sub(dt,30)
    )t where max_chenmo<dt and end_dt=max_chenmo
) ,z2 as (
    select deviceid,sum(chenmodays) chenmodays from z group by deviceid
) select
         case when chenmodays>=3 and chenmodays<5 then '[3,5)'
              when chenmodays>5 then '[5+)'
              else '[0,3)'
         end flag,
         count(1) num
         from z2 group by flag;
相关推荐
2301_8002561123 分钟前
第九章:空间网络模型(空间网络查询、数据模型、Connected、with Recursive、pgRouting)
网络·数据库·算法·postgresql·oracle
霖霖总总1 小时前
[小技巧19]MySQL 权限管理全指南:用户、角色、授权与安全实践
数据库·mysql·安全
heartbeat..6 小时前
Spring AOP 全面详解(通俗易懂 + 核心知识点 + 完整案例)
java·数据库·spring·aop
麦聪聊数据8 小时前
MySQL并发与锁:从“防止超卖”到排查“死锁”
数据库·sql·mysql
AC赳赳老秦9 小时前
DeepSeek 私有化部署避坑指南:敏感数据本地化处理与合规性检测详解
大数据·开发语言·数据库·人工智能·自动化·php·deepseek
YMatrix 官方技术社区9 小时前
YMatrix 存储引擎解密:MARS3 存储引擎如何超越传统行存、列存实现“时序+分析“场景性能大幅提升?
开发语言·数据库·时序数据库·数据库架构·智慧工厂·存储引擎·ymatrix
辞砚技术录10 小时前
MySQL面试题——索引2nd
数据库·mysql·面试
linweidong11 小时前
C++thread pool(线程池)设计应关注哪些扩展性问题?
java·数据库·c++
欧亚学术11 小时前
突发!刚刚新增17本期刊被剔除!
数据库·论文·sci·期刊·博士·scopus·发表
黑白极客12 小时前
怎么给字符串字段加索引?日志系统 一条更新语句是怎么执行的
java·数据库·sql·mysql·引擎