Вы можете назначить группы, и тогда это проблема пробелов и островов:
with t as (
select t.*,
(case when value >= 0 and value <= 5 then 'Group 1'
when value >= 6 and value <= 10 then 'Group 2'
when value >= 11 and value <= 15 then 'Group 3'
when value >= 16 and value <= 20 then 'Group 4'
end) as grp
from t
)
select row_number() over (order by min(id)) as new_id,
grp, min(value), max(value), avg(value)
from (select t.*,
row_number() over (order by id) as seqnum,
row_number() over (partition by grp order by id) as seqnum_g
from t
) t
group by (seqnum - seqnum_g), grp;
Я почти уверен, что вы можете упростить логику case
до:
with t as (
select t.*,
(case when value <= 5 then 'Group 1'
when value <= 10 then 'Group 2'
when value <= 15 then 'Group 3'
else 'Group 4'
end) as grp
from my_table
)