Skip to content
Advertisement

how to find which rows are being overlap while finding overlap b/w the dates

How can we find the between the overlap lap b/w the dates . overlap means when start date and end date are within same range for below example row 1 has no over lap. Row 2to 5 can be considered as one set of over lap as there start date and end are over lap with themselves Row 6 & 7 can be considered as one set of over lap for eg. row 6 & 7 –> start date of row 7 is in same range with respect to end date of row 6 so it becomes an overlap

Once overlap is found then and need to find out min(start date) and max(end date) and want to assign a unique id to each overlap and in the S.NO column should show which rows are overlapped.

Below is the I/p and O/p enter image description here

enter image description here

Advertisement

Answer

Based on this answer (which contains a detailed explanation of the query), you can change the final part to generate groups of overlapping rows and then aggregate using LISTAGG to get the delimited sno values:

Which, for the sample data:

Outputs:

ID | START_DATE          | END_DATE            | SNOS   
-: | :------------------ | :------------------ | :------
 1 | 2020-10-11 00:00:00 | 2020-10-11 00:00:00 | 1      
 1 | 2020-11-04 00:00:00 | 2020-12-20 00:00:00 | 2,3,4,5
 1 | 2021-01-01 00:00:00 | 2021-03-25 00:00:00 | 6,7    

db<>fiddle here


From Oracle 12c you can use MATCH_RECOGNIZE for a much simpler query:

db<>fiddle here

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement