sql(oracle)计算重叠间隔的数量

时间:2018-09-24 17:58:52

标签: sql oracle intervals

我有以下问题:

在Oracle sql数据库中提供下表test

+----+------+-------+------+
| id | name | start | stop |
+----+------+-------+------+
| 1  |   A  |   1   |  5   |
+----+------+-------+------+
| 2  |   A  |   2   |  6   |
+----+------+-------+------+
| 3  |   A  |   5   |  8   |
+----+------+-------+------+
| 4  |   A  |   9   |  10  |
+----+------+-------+------+
| 5  |   B  |   3   |  6   |
+----+------+-------+------+
| 6  |   B  |   4   |  8   |
+----+------+-------+------+
| 7  |   B  |   1   |  2   |
+----+------+-------+------+

对于所有具有相同n_overlap的{​​{1}},即:

,我想找到重叠间隔的数量(包括端点)[{1,}}
id

1 个答案:

答案 0 :(得分:2)

一种方法使用相关的子查询:

select t.*,
       (select count(*)
        from test t2
        where t2.name = t.name and
              t2.start < t.end and
              t2.end > t.start
       ) as num_overlaps
from test t;
相关问题