Hive面试题系列-求用户最大连续活跃天数 2

一、题目:求用户最大连续活跃天数

1. 表结构:

create table `lotable`(
	uid int,
	dt string)
ROW FORMAT DELIMITED
FIELDS TERMINATED BY '|'
LINE TERMINATED BY '\n';	

2. 用户表数据:

1|2022-07-11
1|2022-07-12
1|2022-07-13
1|2022-07-14
1|2022-07-15
1|2022-07-16
1|2022-07-17
1|2022-07-18
2|2022-07-12
2|2022-07-13
2|2022-07-14
2|2022-07-15
2|2022-07-16
2|2022-07-21
2|2022-07-22
3|2022-08-01
3|2022-08-02
3|2022-08-03
3|2022-08-04
3|2022-08-05
3|2022-08-06
3|2022-08-07
3|2022-08-08
3|2022-08-09
3|2022-08-10
3|2022-08-11

3. SQL代码实现逻辑:

select
	t3.uid,
	max(t3,cnt)
from (	
	select 
		t2.uid,
		t2.group_id,
		count(*) as cnt
	from (	
	select
		t1.uid,
		t1.dt,
		t1.rn,
		date_sub(t1.dt,t1.rn) group_id
	from (	
		select
		uid,
		dt,
		row_number() over(partition by id order by dt) rn
		from logtable
		group by uid,dt
	  ) t1
	) t2	  
	  group by id,group_id
) t3
group by uid ;  

Hive面试题系列-求用户最大连续活跃天数 2_第1张图片

你可能感兴趣的:(Hive,hive,hadoop,数据仓库)