I have below data set with me. I want to ensure that for any particular ID start_date, end_date and the period in between these two dates are not overlapping with dates of any other ID.
ID Start_Date End_Date
101 01-01-2001 31-01-2001
102 01-02-2001 28-02-2001
103 26-02-2001 31-03-2016
104 15-03-2001 30-04-2001
105 01-05-2002 31-05-2002
106 05-12-2002 31-12-2002
107 15-12-2002 05-01-2003
To do this I created below query:
select id,start_date,end_date,
case
when
end_date < max(end_date) over(order by start_date rows unbounded preceding)
then 'overlapping'
when
start_date < max(end_date) over(order by start_date rows unbounded preceding)
then 'overlapping'
else 'non-overlapping'
end as FLAG from table
I am getting below output having all flag as 'overlapping' which is not correct. I think 'rows unbounded preceding' is taking current row also in calculation: Can you please let me know where I am wrong:
ID Start_Date End_Date Flag
101 01-01-2001 31-01-2001 Overlapping
102 01-02-2001 28-02-2001 Overlapping
103 26-02-2001 31-03-2016 Overlapping
104 15-03-2001 30-04-2001 Overlapping
105 01-05-2002 31-05-2002 Overlapping
106 05-12-2002 31-12-2002 Overlapping
107 15-12-2002 05-01-2003 Overlapping
rows unbounded preceding
includes the current row, it's a shortcut forrows between unbounded preceding and current row
. Can you show what exact result should be returned? – dnoeth