子选择的时间段

问题描述:

我有2列时间戳:子选择的时间段

  1. START(2015年11月18日10时00例如)
  2. STOP
  3. (例如2015年11月18日22:00)。

我也有通过日历(事件,忙),其具有3列:

  1. What(VARCHAR(50))(例如咖啡面包机k)
  2. START(时间戳)(例如, 2015年11月18日10:55)
  3. STOP(时间戳)(例如,2015年11月18日11:55)

我怎样才能返回所有的空闲时间段为单时间戳
即。

free time1: 2015-11-18 10:00 - 2015-11-18 10:55 
free time2: 2015-11-18 11:55 - 2015-11-18 22:00 

作为单个postgresql查询?

也许你只是想not exists

select ts.* 
from timestamps ts 
where not exists (select 1 
        from calendar c 
        where ts.start <= c.end and 
         ts.end >= c.start 
       ); 

您可能需要调整的不平等是严格的不平等,这取决于你如何定义重叠。注意:您也可以使用overlaps关键字,但我不记得它是否包含终点,这就是为什么我明确写出逻辑的原因。

+0

但是,如果我每天有更多taks?如何找到所有的差距? – blackmoon

+0

@blackmoon。 。 。这应该找到所有的差距。 –