背景
面试和工作中经常遇到,用sql/hive求出连续N天登录或者连续N天销售额破万的记录等。
数据准备
--新建表与导入数据
use test;
create table if not exists log_info
(uid string,
log_date string)
row format delimited fields terminated by ',';
insert into table log_info values('A','20210901'),
('A','20210902'),('A','20210903')
,('B','20210901'),('B','20210903')
,('v','20210830'),('K','20210902')
,('C','20210903'),('B','20210902')
,('D','20210905');
hive> select * from log_info;
OK
A 20210901
A 20210902
A 20210903
B 20210901
B 20210903
v 20210830
K 20210902
C 20210903
B 20210902
D 20210905
思路一
--先将uid对组内的登录时间升序排名rank1,然后将日期与rank1相减。
--因为这两个标签都是按照