尚学堂 讲解视频
id dt
1001 2021-08-01
1001 2021-08-02
1001 2021-08-03
1001 2021-08-05
1001 2021-08-06
1001 2021-08-07
1001 2021-08-10
1001 2021-08-12
1002 2021-08-01
1002 2021-08-02
1002 2021-08-03
1002 2021-08-07
1002 2021-08-09
1002 2021-08-11
1002 2021-08-13
1002 2021-08-15
select
id,
dt,
rank() over(partition by id order by dt) rk
from tx;
select
id,dt,
date_sub(dt,rk) flag
from t1;t2
select
id,
flag,
count(*) days
from t2
group by id,flag;
select
id ,
flag ,
days,
rank() over(partition by id order by flag) newFlag
from t3;
select
id,
days,
date_sub(flag, newFlag) flag
from t4;
select
id,
flag,
sum( days)+count(*)-1 days
from t5
group by id,flag; t6
select
id,
max( days)
from t6
select
id,
dt,
lag(1,dt, '1970-01-01 ') over(partition by id order by dt) lagDt
from tx;t1
select
id,
dt,
datediff(dt,lagDt) dtDiff
from t1;t2
select
id,
dt,
sum(if(dtDiff>2,1,0)) over(partition by id order by dt) flag
from t2;t3
select
id,
flag,
datediff(max(dt) ,min(dt)) +1
from t3
group by id,flag;