2017-04-25 107 views
0

我在我的Rails應用程序的查詢,看起來像這樣:PostgreSQL tsrange - 重疊忽略等效範圍?

# Returns any records whose period intersect the specified time 
# @see https://www.postgresql.org/docs/9.3/static/rangetypes.html 
# @see https://www.postgresql.org/docs/9.3/static/functions-range.html 
# @note The '()' means exclude both sides of the range 
# 
# @param period_start [DateTime,Time] Start of the range 
# @param period_end [DateTime,Time] End of the range 
scope :overlapping, ->(period_start, period_end) { 
    where(
    "#{table_name}.period && tsrange(:period_start, :period_end, '()')", 
    period_start: Time.at(period_start.to_i), # Caps precision to the second 
    period_end: Time.at(period_end.to_i) # Caps precision to the second 
).distinct 
} 

然而,這裏的問題是,誰具有相同的週期由period_startperiod_end創建的範圍內的任何記錄都沒有回來。這是爲什麼?我認爲重疊檢查是否有任何交集?

回答

1

但是,這裏的問題是不會返回任何具有由period_start和period_end創建的範圍的SAME句點的記錄。

這是不正確的。不知道你的問題是什麼,但那不是。

SELECT 
    tsrange(x,y), 
    tsrange(x,y) && tsrange(x,y) AS overlaps 
FROM (VALUES 
    ('yesterday'::timestamp, 'today'::timestamp) 
) AS t(x,y); 
        tsrange     | overlaps 
-----------------------------------------------+---------- 
["2017-04-24 00:00:00","2017-04-25 00:00:00") | t 
(1 row) 
+0

熱潮!在我的測試中,這是一個問題,我測試的那個人的範圍相當於'period_start..period_start'而不是'period_start..period_end'。謝謝 :) – trevorhinesley