mysql - Simplify the timeline in SQL(Netezza) -
i want summarize/simplify(i don't know call) timeline.
so have id timeline. trying rid of overlap timeline within same id.
here example of data. have:
id start_time end_time 1 b 1 c d 1 e f 1 g h
can see picture, [a,b],[c,d],[e,f]
overlap each other , [g,h]
disjoint want [a,f]
, [g,h]
. want:
id start_time end_time 1 f 1 g h
i think @sha.t close. problem in multiple overlaps break down. might have turn multiple step process
step 1 (make sample table):
create temp table stack ( id integer ,start_time timestamp ,end_time timestamp ) insert stack values(1, date('2020-01-01'),date('2020-01-01') + interval '3 hours'); insert stack values(1,date('2020-01-01') + interval '2 hours',date('2020-01-01') + interval '4 hours'); insert stack values(1,date('2020-01-01') + interval '3.5 hours',date('2020-01-01') + interval '5 hours'); insert stack values(1,date('2020-01-01') + interval '5.5 hours',date('2020-01-01') + interval '6.5 hours'); insert stack values(1,date('2020-01-01') + interval '7.5 hours',date('2020-01-01') + interval '9.5 hours'); insert stack values(1,date('2020-01-01') + interval '8.5 hours',date('2020-01-01') + interval '10.5 hours');
step 2 (find single overlaps):
create temp table stack2 select id, ps2 start_time, max(e) end_time ( select t1.id, t1.start_time s, max(t1.end_time) e, max(t2.start_time) ps, max(t2.end_time) pe ,case when pe between s , e ps else s end ps2 stack t1 join stack t2 on t1.start_time > t2.start_time group t1.id, t1.start_time) dt group id, ps2 order ps2
step 3 (consolidate double overlaps):
select id, ps2 start_time, max(e) end_time ( select t1.id, t1.start_time s, max(t1.end_time) e, max(t2.start_time) ps, max(t2.end_time) pe ,case when pe between s , e ps else s end ps2 stack2 t1 join stack2 t2 on t1.start_time > t2.start_time group t1.id, t1.start_time) dt group id, ps2 order ps2
Comments
Post a Comment