Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

SQL Lead & Lag to find gap between dates

nvl2lifeAug 12 2013 — edited Aug 12 2013

i have a table with two columns event_start and event_end :

CREATE TABLE MYBATCHTAB

(

  EVENT_START  DATE                             NOT NULL,

  EVENT_END    DATE                             NOT NULL

);

and my data is :

Insert into MYBATCHTAB

   (EVENT_START, EVENT_END)

Values

   (TO_DATE('08/12/2013 22:45:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('08/12/2013 23:55:00', 'MM/DD/YYYY HH24:MI:SS'));

Insert into MYBATCHTAB

   (EVENT_START, EVENT_END)

Values

   (TO_DATE('08/12/2013 15:30:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('08/12/2013 17:00:00', 'MM/DD/YYYY HH24:MI:SS'));

Insert into MYBATCHTAB

   (EVENT_START, EVENT_END)

Values

   (TO_DATE('08/12/2013 16:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('08/12/2013 17:30:00', 'MM/DD/YYYY HH24:MI:SS'));

Insert into MYBATCHTAB

   (EVENT_START, EVENT_END)

Values

   (TO_DATE('08/12/2013 20:00:00', 'MM/DD/YYYY HH24:MI:SS'), TO_DATE('08/12/2013 22:00:00', 'MM/DD/YYYY HH24:MI:SS'));

COMMIT;

Event StartEvent End
08/12/2013 15:30:0008/12/2013 17:00:00
08/12/2013 16:00:0008/12/2013 17:30:00
08/12/2013 20:00:00'08/12/2013 22:00:00
08/12/2013 22:45:0008/12/2013 23:55:00

and i want to find the first whole start - end period in this example start : 15:30 - end 17:30 (merging  record 1&2 )

but not the third one for example not 15.30 - 22:00 or not 15.30 23:55 because there are gaps between end dates.

how can i do this using lead&lag ? 

I'm not sure if this is the best approach

Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 9 2013
Added on Aug 12 2013
9 comments
3,601 views