I am trying to compare two columns in an SQL table based on two dates. The table is set up so the each person in table A has a foreign key to another table B, each person can have multiple entries in table B. Each entry in table B contains a start date and end date and I am trying to grab each person that has an overlap in table B so the Start Date of one of the entries is before the End Date of any other entry.
So John Doe in table A has two entries in table B where the start day of Entry 1 is April 5th 2015 and end date of April 6th 2016 and the second entry is January 10th 2016 and ends January 10th 2017, so I would want to include this person in my result set.
However Jane Doe in table A as two entries in table B
Entry 1: SD April 10th 2014 End April 10th 2015
Entry 2: SD May 11th 2015 End May 11th 2016
So I would not like to I include Jane Doe in my result set.
I am thinking I need to use two nested for loop in the where part of the select statement flipping a variable back and forth depending on whether or not I want to include this person.
Something along the lines of
Select * from A
where
(reset variable
for b in
(select * from b.id = A.b_id); loop
for btwo in
(select * from b.id = A.b_id); loop
// set variable based on start / end date
// if I want to include set var = 1 else 0
end loop;
end loop;)
variable = 1;