What is a simple and efficient way to find rows with time-interval overlaps in SQL?

后端 未结 5 1550
清歌不尽
清歌不尽 2020-11-30 00:22

I have two tables, both with start time and end time fields. I need to find, for each row in the first table, all of the rows in the second table where the time intervals in

5条回答
  •  一整个雨季
    2020-11-30 00:48

    "solutions from other platforms may be of interest also."

    SQL Standard defines OVERLAPS predicate:

    Specify a test for an overlap between two events.

     ::=
         OVERLAPS  
    

    Example:

    SELECT 1
    WHERE ('2020-03-01'::DATE, '2020-04-15'::DATE) OVERLAPS 
          ('2020-02-01'::DATE, '2020-03-15'::DATE) 
    -- 1
    

    db<>fiddle demo

提交回复
热议问题