在表格中找到重叠的地址

时间:2016-02-18 18:53:33

标签: sql

我有一张地址表说:

address id person_id start_date stop_date address 
1          123       01-JAN-15  01-JUN-15 india
2          123       01-MAY-15  null      Russia
3          321       01-JAN-15  01-JUN-15 us
4          321       01-MAY-15  null      india

我想查找所有记录(地址ID),这些记录(地址ID)具有相同person_id的重叠日期。例如address_id 1和4.可能介于jan和jun之间

1 个答案:

答案 0 :(得分:1)

像...一样的东西。

select address_id 
FROM table1 t1 
WHERE EXISTS(SELECT null 
             FROM table1 t2 
             WHERE t1.person_id=t2.person_id 
             AND t1.start_date BETWEEN t2.start_date AND t2.end_date)