Oracle MATCH_RECOGNIZE: ONE ROW PER MATCH vs ALL ROWS PER MATCH
In the match_recognize clause, it is possible to specify if a matched pattern produces one record (one row per match) or returns a record for all rows that were matched (all rows per match).
The differences between using these claues is demonstrated with the following example.
First, we need a table with some test data. Especially note that there are four groups where where of «consecutive» values for nr (the second and fourth group consists of one row only):
create table tq84_consecutive_rows (
id number,
txt varchar2(20)
);
insert into tq84_consecutive_rows values ( 1, 'one' );
insert into tq84_consecutive_rows values ( 2, 'two' );
insert into tq84_consecutive_rows values ( 3, 'three' );
insert into tq84_consecutive_rows values ( 5, 'five' );
insert into tq84_consecutive_rows values ( 8, 'eight' );
insert into tq84_consecutive_rows values ( 9, 'nine' );
insert into tq84_consecutive_rows values (10, 'ten' );
insert into tq84_consecutive_rows values (11, 'eleven');
insert into tq84_consecutive_rows values (20, 'twenty');
The following query uses one row per match (which is the default and therefore not explicitetly stated) to find the first and last (min and max) id of each group of consecutive rows:
select * from tq84_consecutive_rows
match_recognize (
order by id
measures
min(id) start_id,
max(id) end_id
pattern (R C*)
define C as id = prev(id) + 1
);
--
-- START_ID END_ID
-- ---------- ----------
-- 1 3
-- 5 5
-- 8 11
-- 20 20