---连续活跃登陆的用户指至少连续2天都活跃登录的用户
---解决类似场景的问题
CREATE TABLE test5active(
dt string,
user_id string,
age int)
ROW format delimited fields terminated BY ',';
INSERT INTO TABLE test5active VALUES
('2019-02-11','user_1',23),('2019-02-11','user_2',19),
('2019-02-11','user_3',39),('2019-02-11','user_1',23),
('2019-02-11','user_3',39),('2019-02-11','user_1',23),
('2019-02-12','user_2',19),('2019-02-13','user_1',23),
('2019-02-15','user_2',19),('2019-02-16','user_2',19);
思路一:
1、因为每天用户登录次数可能不止一次,所以需要先将用户每天的登录日期去重。
2、再用row_number() over(partition by _ order by _)函数将用户id分组,按照登陆时间进行排序。
3、计算登录日期减去第二步骤得到的结果值,用户连续登陆情况下,每次相减的结果都相同。
4、按照id和日期分组并求和,筛选大于等于2的即为连续活跃登陆的用户。
第一步:用户登录日期去重
select DISTINCT dt,user_id from test5active;
第二步:用row_number() over()函数计数
select
t1.user_id,t1.dt,
row_number() over(partition by t1.user_id order by t1.dt) day_rank
from
(
select DISTINCT dt,user_id from test5active
)t1;
第三步:日期减去计数值得到结果
select
t2.user_id,t2.dt,date_sub(t2.dt,t2.day_rank) as dis
from
(
select
t1.user_id,t1.dt,
row_number() over(partition by t1.user_id order by t1.dt) day_rank
from
(
select DISTINCT dt,user_id from test5active
)t1
)t2;
第四步:根据id和结果分组并计算总和,大于等于2的即为连续登陆的用户,得到 用户id,开始日期,结束日期 ,连续登录天数
select
t3.user_id,min(t3.dt),max(t3.dt),count(1)
from
(
select
t2.user_id,t2.dt,date_sub(t2.dt,t2.day_rank) as dis
from
(
select
t1.user_id,t1.dt,
row_number() over(partition by t1.user_id order by t1.dt) day_rank
from
(
select DISTINCT dt,user_id from test5active
)t1
)t2
)t3 group by t3.user_id,t3.dis having count(1)>1;
最后:连续登陆的用户
select distinct t4.user_id
from
(
select
t3.user_id,min(t3.dt),max(t3.dt),count(1)
from
(
select
t2.user_id,t2.dt,date_sub(t2.dt,t2.day_rank) as dis
from
(
select
t1.user_id,t1.dt,
row_number() over(partition by t1.user_id order by t1.dt) day_rank
from
(
select DISTINCT dt,user_id from test5active
)t1
)t2
)t3 group by t3.user_id,t3.dis having count(1)>1
)t4;
思路二:
使用lag(向前)或者lead(向后)
select
user_id,t1.dt,
lead(t1.dt) over(partition by user_id order by t1.dt) as last_date_id
from
(
select DISTINCT dt,user_id from test5active
)t1;
选出差异等于1的数据
select
distinct t2.user_id
from
(
select
user_id,t1.dt,
lead(t1.dt) over(partition by user_id order by t1.dt) as last_date_id
from
(
select DISTINCT dt,user_id from test5active
)t1
)t2 where datediff(last_date_id,t2.dt)=1;