I have a set of date values in Oracle that I am doing an ORDER BY START_TIME, STOP_TIME
as in the data sample below. The column labeled NEXT_START_TIME, as you can see, is retrieved through the LEAD() function.
START_TIME STOP_TIME NEXT_START_TIME 6/13/2013 5:19 6/13/2013 5:34 6/13/2013 5:19 -- no delay, OK 6/13/2013 5:19 6/13/2013 6:23 6/13/2013 5:39 6/13/2013 5:39 6/13/2013 6:04 6/13/2013 6:23 -- delay? wrong 6/13/2013 6:23 6/13/2013 7:32 6/13/2013 9:18 6/13/2013 9:18 6/13/2013 9:50 6/13/2013 9:44 6/13/2013 9:44 6/13/2013 10:01
This data represents assigned jobs. The difficult task I have is, in the most efficient manner, how to determine if there was any delay in assigning a job. The first entry represents no delay since the NEXT_START_TIME came before the STOP_TIME. Only in cases where the NEXT_START_TIME came after the STOP_TIME can we consider there to be a delay (as in the third row).
The wrinkle is that even though the third row would seem to indicate a delay, it's not correct. Even though the NEXT_START_TIME was 6:23 it needs to be aware of the fact that a previous job was still in progress (see the two highlighted items).
Is there a way to accomplish this without O(N), or worse, iterations? Right now the use of the LEAD() function is very efficient.
Please ask for clarifications if needed since I realize this may be difficult to understand.