【问题标题】:PostgreSQL tsrange -- overlap ignores equivalent ranges?PostgreSQL tsrange - 重叠忽略等效范围?
【发布时间】:2017-04-25 22:39:25
【问题描述】:

我的 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_startperiod_end 创建的范围具有相同句点的记录都不会返回。这是为什么?我认为重叠检查是否有任何交叉点?

【问题讨论】:

    标签: ruby-on-rails postgresql overlap range-types


    【解决方案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)
    

    【讨论】:

    • 轰隆隆!这是我测试中的一个问题,我正在测试的那个范围等于period_start..period_start,而不是period_start..period_end。谢谢你:)
    猜你喜欢
    • 1970-01-01
    • 2017-07-06
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    • 2019-12-07
    • 1970-01-01
    • 2011-05-27
    • 1970-01-01
    相关资源
    最近更新 更多