统计用户连续登陆最大的天数

注意: 断一天 也算连续登陆

 

1、创建建表语句

create table demo(
    id string, 
    dt string
) row format delimited fields terminated by '\t';

 

2、数据准备

1001    2021-08-01
1001    2021-08-02
1001    2021-08-03
1001    2021-08-05
1001    2021-08-06
1001    2021-08-07
1001    2021-08-10
1001    2021-08-12

 

3、数据导入

load data local inpath '/home/hadoop/demo.txt' into table demo;

 

4、sql 语句

统计用户连续登陆最大的天数点击展开全部
select
    id,
    flag,
    datediff(max(dt), min(dt)) + 1 as days
from(
    select
        id,
        dt,
        lag_dt,
        dt_diff,
        sum(if(dt_diff > 2, 1, 0)) over(partition by id order by dt) as flag
    from(
        select
            id,
            dt,
            lag_dt,
            datediff(dt, lag_dt) as dt_diff
        from(
            select 
                id,
                dt,
                lag(dt, 1, '1970-01-01') over(partition by id order by dt) as lag_dt
        from demo
        ) t1
    ) t2
) t3
group by id, flag
having datediff(max(dt), min(dt)) + 1 > 6

 

5、sql 解析

 

上一篇:Kubernetes 中审计策略


下一篇:C#DataTable(转List /JSON/字典 互转)