查找其他范围内的最大范围

时间:2018-07-06 13:53:21

标签: sql postgresql gaps-and-islands

我有一张表,描述为

create table range (
    x int not null,
    y int not null,
    check (x < y)
);

表中填充了这样的范围

insert into range(x,y) values (1,5);
insert into range(x,y) values (2,6);
insert into range(x,y) values (2,3);
insert into range(x,y) values (4,6);
insert into range(x,y) values (2,6);

insert into range(x,y) values (9,10);
insert into range(x,y) values (8,11);
insert into range(x,y) values (7,9);

insert into range(x,y) values (12,15);

我想用某些选择查询表,该表返回最大连续范围。

select ????? from range
x , y
--------------
1 , 6
7 , 11
12, 15 

我需要递归或窗口函数吗?

1 个答案:

答案 0 :(得分:0)

这是一个空白和孤岛的问题。这个想法是找到每个组的开始位置,然后使用累积总和来定义组(“岛屿”)。然后是一个聚合:

select min(x) as x, max(y) as y
from (select r.*,
            sum(isstart) over (order by x range between unbounded preceding and current row) as grp
      from (select r.*,
                   (not exists (select 1
                                from range r2
                                where r2.x < r.x and r2.y >= r.x
                               )
                   )::int as isstart
            from range r
           ) r
     ) r
group by grp
order by min(x);

Here是一个SQL提琴。

注意:range between应该处理多个范围在同一日期开始并开始一个感兴趣期的情况。