PostgreSQL tsrange

编程入门 行业动态 更新时间:2024-10-26 12:22:03
PostgreSQL tsrange - 重叠忽略等效范围?(PostgreSQL tsrange — overlap ignores equivalent ranges?)

我的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创建的范围的记录。 这是为什么? 我认为重叠检查是否有任何交叉点?

I have a query in my Rails app that looks like this:

# 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 }

However, the problem here is that any records who have the SAME period as the range created by period_start and period_end are not returned. Why is that? I thought overlap checked if there was any intersection at all?

最满意答案

但是,这里的问题是不会返回任何具有相同周期作为由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)

However, the problem here is that any records who have the SAME period as the range created by period_start and period_end are not returned.

That's not true. Not sure what your problem is, but that's not it.

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)

更多推荐

本文发布于:2023-07-30 22:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340302.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PostgreSQL   tsrange

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!