HQL编写连续7天登录的总人数
数据如下:
t1表
Uid dt login_status(1登录成功,0异常)
1 2019-07-11 1
1 2019-07-12 1
1 2019-07-13 1
1 2019-07-14 1
1 2019-07-15 1
1 2019-07-16 1
1 2019-07-17 1
1 2019-07-18 1
2 2019-07-11 1
2 2019-07-12 1
2 2019-07-13 0
2 2019-07-14 1
2 2019-07-15 1
2 2019-07-16 0
2 2019-07-17 1
2 2019-07-18 0
3 2019-07-11 1
3 2019-07-12 1
3 2019-07-13 1
3 2019-07-14 1
3 2019-07-15 1
3 2019-07-16 1
3 2019-07-17 1
3 2019-07-18 1
// 建表语句
create table if not exists t1(
uid int,
dt string,
login_status int
)
row format delimited
fields terminated by ' ';
//HQL查询语句如下:
select count(distinct a.uid)
from
(
select uid,datediff(dt,lag(dt,6) over(distribute by uid sort by dt)) as t,login_status
from t1
where login_status=1
) a
where a.t=6