解决类似场景的问题
创建数据
1
2
3
4
5
6
7
8
9
10
11
12
|
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的即为连续活跃登陆的用户。
第一步:用户登录日期去重
1
|
select distinct dt,user_id from test5active; |
第二步:用row_number() over()函数计数
1
2
3
4
5
6
7
|
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; |
第三步:日期减去计数值得到结果
1
2
3
4
5
6
7
8
9
10
11
|
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,开始日期,结束日期,连续登录天数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
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; |
用户id 开始日期 结束日期 连续登录天数
最后:连续登陆的用户
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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(向前)
1
2
3
4
5
6
7
|
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
2
3
4
5
6
7
8
9
10
11
12
|
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; |
到此这篇关于hive-sql查询连续活跃登陆的用户的文章就介绍到这了,更多相关sql查询连续登陆的用户内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://www.cnblogs.com/yangms/p/14179403.html