I'm going to present a solution that uses a recursive common table expression to select the various 'groups' of contiguous rows. It doesn't exactly meet your requirements, because I think this solution is more generically useful, but it can be modified to achieve your requirement.
This solution requires Firebird 3.0 or later; for simplicity I made field1
a BOOLEAN
.
with recursive r as (
select s.id, s.field1, s.field2, s.prev_id,
row_number() over () as group_num
from s
where prev_id is null or prev_id < id - 1
union all
select s.id, s.field1, s.field2, s.prev_id, r.group_num
from r
inner join s
on s.id = r.id + 1
),
s as (
select id, field1, field2,
lag(id) over (order by id) as prev_id
from table1
where field1
),
a as (
select id, field2, group_num,
count(*) over (partition by group_num) as group_count
from r
)
select id, field2, group_num, group_count
from a
order by id
The CTE r
first finds the first row of each group, and then recursively adds the next rows in that group. Be aware: this doesn't work if a group has more than 1024 rows due to recursion limits in Firebird. Each group receives a number, group_num
.
The CTE a
is used to count the number of rows in each group. The CTE s
is used to determine the previous id for each row that matches the condition.
To get the actual result you want, you need to modify the main query to select the first group with 5 or more rows, and fetch the first 5 rows:
-- with clause omitted for brevity, copy it from the previous code block
select field2
from a
where a.group_num = (
select group_num
from a
where group_count >= 5
order by id
fetch first row only
)
order by id
fetch first 5 rows only