match_recognize — Regular Expressions Over Rows


Use Cases

The match_recongnize clause is useful to implement the following use cases:

  • Finding series of consecutive events (see slides)

  • Pattern matching: trend reversal, periodic events, …

  • Top-N per Group (see slides)

Compatibility

Row pattern matching was introduced by SQL:2016 with three optional features: R010 is the basis: this feature requires the match_recognize clause as explained above with the aggregate functions min, max, sum, count, avg. R020 allows using patterns in the over clause to define frames. R030 supports all aggregate functions for row patterns (e.g. stddev_pop, …).

JDBC and ORA-17041 Missing IN or OUT parameter

The character ? is used as placeholder in JDBC but is also a valid literal in the pattern clause (e.g., for non-greedy matching). Per default, the Oracle JDBC driver treats each ? as bind parameter. To use ? as literal in the pattern clause, the Oracle JDBC driver supports two methods:

Statement.setEscapeProcessing(false)

This is a standard JDBC feature that disables any client side interpretation of the SQL string. It doesn't work for PreparedStatements and thus disallows the use of bind variables.

{\?\}

The Oracle JDBC driver support a proprietary escape syntax for the question mark: {\?\}. This character sequence can be used in the pattern clause instead of a question mark. This method works for PreparedStatements.

About the Author

Photo of Markus Winand

Markus Winand teaches efficient SQL—inhouse and online. He minimizes the development time using modern SQL and optimizes the runtime with smart indexing—for that he also published the book SQL Performance Explained.

Markus’ Book

Cover of “SQL Performance Explained”: Squirrel running on grass

The essence of SQL tuning in 200 pages.

Buy from Markus
(paperback and/or PDF)

Buy from Amazon
(paperback only)

Hire Markus

…to answer your current SQL questions.

The quick and easy way to benefit from his extensive knowledge and experience. Learn more»

“modern SQL” by Markus Winand is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.
Legal | Contact | NO WARRANTY | Trademarks | Privacy | CC-BY-NC-ND 3.0 license