HiveSQL연속일 및 연속일 찾기
2326 단어 Hive
참조 키 코드:
select =min(rq), =max(rq),
=max(id1)-min(id1)+1,
=case a.id1-a.id2 when -1 then 0 else max(datediff(d,rq2,rq)) end
from
(
select id1 = datediff(d,'2010-01-01',rq),id2=(select count(1) from tmptable where rq <= a.rq),
rq2=(select max(rq) from tmptable where rq < a.rq),*
from tmptable a
) a
group by a.id1-a.id2
테스트 데이터:
use xxx;
drop table test_serialdate;
create table if not exists test_serialdate (
rq string comment ' '
) stored as rcfile
;
insert into table test_serialdate
select '2019-01-01' as rq from dual union all
select '2019-01-02' as rq from dual union all
select '2019-01-05' as rq from dual union all
select '2019-01-06' as rq from dual union all
select '2019-01-08' as rq from dual union all
select '2019-01-09' as rq from dual union all
select '2019-01-10' as rq from dual union all
select '2019-01-11' as rq from dual union all
select '2019-01-17' as rq from dual union all
select '2019-01-18' as rq from dual ;
코드
select b.gp,b.startdate,b.enddate,b.days,(case when b.gp = 0 then 0 else b.missingdays end)
from
(
select a.gp,min(a.rq) as startdate,max(a.rq) as enddate,
(max(a.id1)-min(a.id1)+1) as days,
max(datediff(a.rq,a.rq2)) as missingdays
from
(
select ta.rq,
datediff(ta.rq,'2019-01-01') as id1, --
nvl(tb.id2,0) as id2, --
tc.rq2, --
nvl((datediff(ta.rq,'2019-01-01')-tb.id2),0) as gp --
from test_serialdate ta
left join
( --
select t11.rq,count(1) as id2
from test_serialdate t11
inner join test_serialdate t12
where t11.rq > t12.rq
group by t11.rq
) tb
on ta.rq = tb.rq
left join
( --
select t21.rq,max(t22.rq) as rq2
from test_serialdate t21
inner join test_serialdate t22
where t21.rq > t22.rq
group by t21.rq
) tc
on ta.rq = tc.rq
) a
group by a.gp
) b
;
후기: 소가 이 문제를 해결하는 핵심은 일수, 대문자 옷이 부족하다는 것이다.