(self) join by time intervals (self) join by time intervals oracle oracle

(self) join by time intervals


Maybe worth a try (also you need to write the most restricting conditions in the end in oracle, don't ask me why or believe me, better do your own performance tests):

SELECT   p.idFROM   periods pWHERE   EXISTS(SELECT * FROM periods q WHERE      (p.startTime BETWEEN q.startTime AND q.endTime      OR p.endTime BETWEEN q.startTime AND q.endTime      OR p.startTime < q.startTime AND p.endTime > q.endTime -- overlapping correction, remove if not needed      ) AND q.type = 'TYPEA'   ) AND p.type = 'TYPEB'ORDER BY   p.id;