简体   繁体   中英

Find overlapping date range from a data set

p_id    book_num conf_num   arrival_dt  departure_dt    create-dt   room_num
353     21807    3328568    19-JUN-15   21-JUN-15       27-JUN-15   2408
353     21807    3328562    18-JUN-15   20-JUN-15       27-JUN-15   2408

In the above example arrival_dt and departure_dt is overlapping for 2 different confirmation numbers for the same room number 2408

also I want to exclude the below set of records where arrival_dt and departure_dt are same

p_id    book_num conf_num   arrival_dt  departure_dt    create-dt   room_num
353     21802    3328508    18-JUN-15   21-JUN-15       27-JUN-15   1909    
353     21802    3328555    18-JUN-15   21-JUN-15       27-JUN-15   1909

Can you please help me with a SQL logic to find these kind of records in the table

The correct logic is that one departs after the other arrivesa and the first arrives before the other departs. You can do this with a self-join or a where clause.

If you just want the records:

select r.*
from records r
where exists (select 1
              from records r2
              where r2.pid = r.pid and
                    r2.arrival_dt >= r.departure_dt and
                    r2.departure_dt <= r.arrival_dt
             );
SELECT tbl.*
FROM table tbl
JOIN table tbl1 ON tbl.p_id= tbl1.p_id
WHERE tbl.Arrival_dt <= tbl1.Departure_dt
    AND tbl.Departure_dt >= tbl1.Arrival_dt

SQL Fiddle

Oracle 11g R2 Schema Setup :

CREATE TABLE TEST ( p_id, book_num, conf_num, arrival_dt, departure_dt, create_dt, room_num ) AS
          SELECT 353, 21807, 3328568, DATE '2015-06-19', DATE '2015-06-21', DATE '2015-06-27', 2408 FROM DUAL
UNION ALL SELECT 353, 21807, 3328562, DATE '2015-06-18', DATE '2015-06-20', DATE '2015-06-27', 2408 FROM DUAL
UNION ALL SELECT 353, 21802, 3328508, DATE '2015-06-18', DATE '2015-06-21', DATE '2015-06-27', 1909 FROM DUAL
UNION ALL SELECT 353, 21802, 3328555, DATE '2015-06-18', DATE '2015-06-21', DATE '2015-06-27', 1909 FROM DUAL
UNION ALL SELECT 353, 21801, 3328444, DATE '2015-06-17', DATE '2015-06-21', DATE '2015-06-27', 2000 FROM DUAL
UNION ALL SELECT 353, 21801, 3328445, DATE '2015-06-18', DATE '2015-06-20', DATE '2015-06-27', 2000 FROM DUAL
UNION ALL SELECT 353, 21803, 3328446, DATE '2015-06-19', DATE '2015-06-20', DATE '2015-06-27', 2001 FROM DUAL
UNION ALL SELECT 353, 21804, 3328447, DATE '2015-06-20', DATE '2015-06-21', DATE '2015-06-27', 2001 FROM DUAL;

Query 1 :

SELECT *
FROM   TEST t
WHERE  EXISTS ( SELECT 'X'
                FROM   TEST x
                WHERE  x.room_num     = t.room_num
                AND    x.arrival_dt   < t.departure_dt
                AND    x.departure_dt > t.arrival_dt
                AND NOT (    x.arrival_dt   = t.arrival_dt
                         AND x.departure_dt = t.departure_dt ) )

Results :

| P_ID | BOOK_NUM | CONF_NUM |             ARRIVAL_DT |           DEPARTURE_DT |              CREATE_DT | ROOM_NUM |
|------|----------|----------|------------------------|------------------------|------------------------|----------|
|  353 |    21807 |  3328568 | June, 19 2015 00:00:00 | June, 21 2015 00:00:00 | June, 27 2015 00:00:00 |     2408 |
|  353 |    21807 |  3328562 | June, 18 2015 00:00:00 | June, 20 2015 00:00:00 | June, 27 2015 00:00:00 |     2408 |
|  353 |    21801 |  3328444 | June, 17 2015 00:00:00 | June, 21 2015 00:00:00 | June, 27 2015 00:00:00 |     2000 |
|  353 |    21801 |  3328445 | June, 18 2015 00:00:00 | June, 20 2015 00:00:00 | June, 27 2015 00:00:00 |     2000 |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM