数据源:
| 有如下登录信息:
userId day
1 2019-05-01
1 2019-05-02
1 2019-05-03
1 2019-05-04
1 2019-05-05
1 2019-05-06
1 2019-05-07
1 2019-05-08
…
2 2019-05-01
2 2019-05-02
2 2019-05-03
2 2019-05-04
2 2019-05-06
2 2019-05-07
2 2019-05-08
2 2019-05-10
2 2019-05-12
需求:
1.求连续登录超过5天的用户
2.求每个用户的连续登录最长天数
实现需求
- 建表userlog
create table userlog(
userId int,
day string
)
row format delimited
fields terminated by '\t';
load data local inpath '/data/userlog' into table userlog ;
2.针对需求1,我们可以分析得知,如果连续登陆超过5天,那么用户的数据必然存在至少连续5天的时间,既然连续那么就是一个等差数列,所以我只需要判断数据中连续五行中的第一行和最后一行差值为4天
select sc.userId from(
select userId,unix_timestamp(day,'yyyy-MM-dd')-unix_timestamp(lag(day,4) over(distribute by userid s