>我有一个表描述为
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
我需要递归函数还是窗口函数?
这是一个差距和孤岛问题。 这个想法是找到每个组的开始位置,然后使用累积总和来定义组("孤岛"(。 然后聚合:
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);
这是一个SQL小提琴。
注意:range between
应处理多个范围从同一日期开始并开始感兴趣期的情况。