相关疑难解决方法(0)

PostgreSQL查询检测重叠时间范围

我在PostgreSQL 9.2中有一个表,看起来像这样(简化):

CREATE TABLE my_features
(
  id integer NOT NULL,
  feature_id integer NOT NULL,
  begin_time timestamp NOT NULL,
  end_time timestamp
)
Run Code Online (Sandbox Code Playgroud)

对于每个feature_id,可能有多行,其时间范围由begin_time/end_time指定.它们可能重叠,但这种情况相对较少.我正在寻找一种快速查找所有具有/没有任何重叠的feature_ids的方法.

我尝试使用窗口函数执行此操作,如下所示:

SELECT feature_id, bool_or(end_time > lead(begin_time) OVER ts_win) OVER ts_win AS overlaps_any
FROM my_features
WINDOW ts_win AS (PARTITION BY feature_id ORDER BY begin_time)
Run Code Online (Sandbox Code Playgroud)

...但这不起作用:

ERROR:  window function calls cannot be nested
Run Code Online (Sandbox Code Playgroud)

算法很简单:通过begin_time对给定feature_id的行进行排序,并检查是否有end_time>下一个begin_time(如果有的话).我怀疑必须有一个简单的方法来做到这一点,也许是使用tsrange函数,但现在似乎无法找到它.

sql postgresql

13
推荐指数
1
解决办法
9708
查看次数

标签 统计

postgresql ×1

sql ×1