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;
相关推荐
玄同7651 小时前
SQLite + LLM:大模型应用落地的轻量级数据存储方案
jvm·数据库·人工智能·python·语言模型·sqlite·知识图谱
吾日三省吾码1 小时前
别只会“加索引”了!这 3 个 PostgreSQL 反常识优化,能把性能和成本一起打下来
数据库·postgresql
chian-ocean1 小时前
百万级图文检索实战:`ops-transformer` + 向量数据库构建语义搜索引擎
数据库·搜索引擎·transformer
小Tomkk1 小时前
数据库 变更和版本控制管理工具 --Bytebase 安装部署(linux 安装篇)
linux·运维·数据库·ci/cd·bytebase
qq_12498707532 小时前
基于JavaWeb的大学生房屋租赁系统(源码+论文+部署+安装)
java·数据库·人工智能·spring boot·计算机视觉·毕业设计·计算机毕业设计
倒流时光三十年2 小时前
SpringBoot 数据库同步 Elasticsearch 性能优化
数据库·spring boot·elasticsearch
码农小卡拉3 小时前
深入解析Spring Boot文件加载顺序与加载方式
java·数据库·spring boot
怣503 小时前
MySQL多表连接:全外连接、交叉连接与结果集合并详解
数据库·sql
wjhx3 小时前
QT中对蓝牙权限的申请,整理一下
java·数据库·qt
冰暮流星3 小时前
javascript之二重循环练习
开发语言·javascript·数据库