2015-10-18 83 views
1

我有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查詢?

回答

1

也許你只是想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。 。 。這應該找到所有的差距。 –