Search notes:

Oracle MATCH_RECOGNIZE: Select first row

The following example demonstrates how match_recognize can be «abused» to select the «first» record in a table or resultset.
First, we need some test data:
create table tq84_test_data (id integer, letter char(1));
insert into tq84_test_data
select
   level,
   chr(64+level)
from
   dual connect by level < 10;
In the actual query, we define the pattern variable ptrn that matches each row (as 1=1).
Then, in the pattern clause, we use the start (ptrn*) to match as many rows as possible, i. e. the entire result set.
Finally, we apply first(…) in the measures clause to return values of the «first» (matched) record:
select
   *
from
   tq84_test_data
match_recognize (
   order by id
   measures
      first(id    ) as id,
      first(letter) as letter
   pattern (ptrn*)
   define
      ptrn as 1=1
);
Of course, the same result can be queried with the fetch first row only clause way easier:
select
   id,
   letter
from
   tq84_test_data
order by
   id
fetch first row only;

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1758207171, '216.73.216.150', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/Oracle/SQL/select/match_recognize/examples/first-row(74): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78