我的Rails应用程序中有一个查询,如下所示:
# 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_start
和period_end
创建的范围的记录。这是为什么?如果有任何交叉点,我认为重叠检查了吗?
答案 0 :(得分:1)
但是,这里的问题是,不会返回任何具有相同周期作为period_start和period_end创建的范围的记录。
那不是真的。不确定你的问题是什么,但那不是它。
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)