By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
select version();
version() |
---|
8.0.23 |
create table logs (log_id int);
insert into logs values (1), (2), (4), (5), (6), (8), (10), (12), (23), (24);
select start_id, min(end_id) as end_id
from (
select t1.log_id as start_id
from logs as t1
left join logs as t2
on t1.log_id-1 = t2.log_id
where t2.log_id is null
) tt_start
join (
select t1.log_id as end_id
from logs as t1
left join logs as t2
on t1.log_id+1 = t2.log_id
where t2.log_id is null
) tt_end
where start_id<=end_id
group by start_id;
start_id | end_id |
---|---|
1 | 2 |
4 | 6 |
8 | 8 |
10 | 10 |
12 | 12 |
23 | 24 |
select min(log_id), max(log_id)
from
(
select log_id, log_id - row_number() over (order by log_id) as grp
from logs
) grouped
group by grp
order by grp;
min(log_id) | max(log_id) |
---|---|
1 | 2 |
4 | 6 |
8 | 8 |
10 | 10 |
12 | 12 |
23 | 24 |