如何从数据库中选择重叠的值对

发布于 2024-10-13 14:12:49 字数 517 浏览 1 评论 0原文

在我的 postgres 数据库中,我有一个包含 3 列的表,如下所示:

start | end  | sorce
17    | 23   | A
150   | 188  | A
200   | 260  | A
19    | 30   | B
105   | 149  | B
199   | 220  | B
...

我想选择来自不同源 A 和 B 的区域(从开始到结束)重叠的所有行。

更新:

从 postgres 8.4 版开始,可以使用 窗口函数。它比连接或子选择方法快得多。 链接到 postgres wiki

In my postgres db I have a table with 3 columns like this:

start | end  | sorce
17    | 23   | A
150   | 188  | A
200   | 260  | A
19    | 30   | B
105   | 149  | B
199   | 220  | B
...

I would like select all row where the regions (start to end) from the differed sources A and B are overlapping.

UPDATE:

From postgres version 8.4 it is posible to solve the problem with window functions. It is much more faster than the join or subselect methodes. Link to postgres wiki.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼眸里的那抹悲凉 2024-10-20 14:12:49

这是一种强力方法(我将您的列 range_start 和 range_end 重命名,以避免与保留字“end”冲突):

select *
from t cross join t t2
where t2.source <> t.source
      and box(point(t2.range_start,t2.range_start),point(t2.range_end,t2.range_end))
            && box(point(t.range_start,t.range_start),point(t.range_end,t.range_end))

或者

select *
from t
where exists (select 1 from t t2
              where t2.source <> t.source and box(point(t2.range_start,t2.range_start),point(t2.range_end,t2.range_end))
                  && box(point(t.range_start,t.range_start),point(t.range_end,t.range_end)))

您应该能够使用 GiST 索引,这可能会提高效率(seq scan +索引扫描):

create index t_range_idx on t using gist (box(point(range_start,range_start),point(range_end,range_end))

此函数可以通过清理 SQL 来帮助理解:

create function range(not_before int, not_after int) returns box
   strict immutable language sql
   as $ select box(point($1,$1),point($2,$2)) $;

使用此函数,您可以编写:

select * from t where range(range_start,range_end) && range(10,20);

并注意 框 && box 运算符表示“重叠”。

This works as a brute-force approach (I renamed your columns range_start and range_end to avoid conflict with the reserved word "end"):

select *
from t cross join t t2
where t2.source <> t.source
      and box(point(t2.range_start,t2.range_start),point(t2.range_end,t2.range_end))
            && box(point(t.range_start,t.range_start),point(t.range_end,t.range_end))

or

select *
from t
where exists (select 1 from t t2
              where t2.source <> t.source and box(point(t2.range_start,t2.range_start),point(t2.range_end,t2.range_end))
                  && box(point(t.range_start,t.range_start),point(t.range_end,t.range_end)))

You should then be able to use a GiST index which may make this a more efficient (seq scan + index scan):

create index t_range_idx on t using gist (box(point(range_start,range_start),point(range_end,range_end))

This function might aid understanding by clearing up the SQL:

create function range(not_before int, not_after int) returns box
   strict immutable language sql
   as $ select box(point($1,$1),point($2,$2)) $;

With this, you can write:

select * from t where range(range_start,range_end) && range(10,20);

and note that the box && box operator means "overlaps".

2024-10-20 14:12:49

如果您想要所有配对,则使用经典重叠测试 INNER JOIN A 和 B

a.start < b.end and b.start < a.end

,即,

select a.start a_start, a.end a_end, b.start b_start, b.end b_end
from tbl a
inner join tbl b on a.start < b.end and b.start < a.end and b.source = 'B'
where a.source = 'A'

如果您不是指源字面上'A'和'B',只是它们不同,您可以使用根据

select a.start a_start, a.end a_end, b.start b_start, b.end b_end
from tbl a
inner join tbl b on a.start < b.end and b.start < a.end and a.source <> b.source

您对重叠的定义,将 <<= 交换(两次)

  • < : 10-20 会 < em>不重叠 20-30
  • <= : 10-20 确实重叠 20-30

If you want all pairings, then INNER JOIN A and B using the classic overlap test

a.start < b.end and b.start < a.end

i.e

select a.start a_start, a.end a_end, b.start b_start, b.end b_end
from tbl a
inner join tbl b on a.start < b.end and b.start < a.end and b.source = 'B'
where a.source = 'A'

If you do not mean sources literally 'A' and 'B', just that they are different, you can use the below instead

select a.start a_start, a.end a_end, b.start b_start, b.end b_end
from tbl a
inner join tbl b on a.start < b.end and b.start < a.end and a.source <> b.source

Depending on your definition of overlapping, swap the < with <= (both times)

  • < : 10-20 does not overlap 20-30
  • <= : 10-20 does overlap 20-30
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文