SlideShare a Scribd company logo
SQL Pattern Recognition
(boldly go beyond analytical)
from 12.1
Lucas Jellema speaks at conferences and user group events, writes blog articles
(on the AMIS Technology Blog) and has published two books with Oracle
Press (on Oracle SOA Suite). His interests range from client side UI and
JavaScript through integration middleware to Database development and
platform design. Creative usages of SQL and PL/SQL are among his favorite
pastimes. In his day time job, Lucas is CTO and architecture consultant at
AMIS in The Netherlands and is affiliated with the Dutch Oracle User Group
(OGh).
Lucas Jellema
@lucasjellema
Oracle ACE Director
Patterns
• Special, significant sequences of data
Pattern Matching
• Discover special patterns in [potentially pretty
big] data sets
• Crucial requirement: records have to be
ordered in some way
– Frequently by time [of observation]
– Could be by the location along some axis
Presenting: Modern Art
• “Who’s afraid of red, yellow and blue”
– Barnett Newman, Stedelijk Museum, Amsterdam
Find art in the car park
• Find if we have cars parked according to the
red-yellow-blue pattern of the painting
Parking
Space
Car
color
Analytical Functions
• Solution with Lag or Lead
• With LEAD it is easy to compare a row with its successor(s)
– As long as the pattern is fixed, LEAD will suffice
with look_ahead_cars as
( SELECT c.* -- for each car, find next and next after that
, lead(car_color,1) over (order by parking_space) next_color
, lead(car_color,2) over (order by parking_space) sec_nxt_color
FROM parked_cars c
)
select parking_space
from look_ahead_cars
where car_color ='red' –- for each red car
and next_color ='yellow' -- check if next is yellow
and sec_nxt_color='blue' –- and the one after that is blue
Match Recognize
• New operator in 12c: MATCH_RECOGNIZE
– Specifically provided for pattern matching
– Pretty fast and very versatile
Match Recognize
• New operator in 12c: MATCH_RECOGNIZE
– Specifically provided for pattern matching
SELECT * -- produces columns from parked_cars and from match_recognize
FROM parked_cars -- record set to find pattern in
MATCH_RECOGNIZE
(
ORDER BY parking_space -- specify ordering of records for pattern
MEASURES RED.parking_space AS red_space -- results to be produced
, MATCH_NUMBER() AS match_num -- umptieth match
ALL ROWS PER MATCH –- all records in pattern or only the first
PATTERN (RED YELLOW BLUE) –- the pattern to locate
DEFINE –- row conditions to be used in pattern
RED AS RED.car_color ='red', –- match on row with red car
YELLOW AS YELLOW.car_color ='yellow', –- match on yellow car
BLUE AS BLUE.car_color ='blue‘–- match on blue car
) MR
ORDER
BY MR.red_space
, MR.parking_space
Match Recognize
• New operator in 12c: MATCH_RECOGNIZE
– Specifically provided for pattern matching
SELECT * -- produces columns from parked_cars and from match_recognize
FROM parked_cars -- record set to find pattern in
MATCH_RECOGNIZE
(
ORDER BY parking_space -- specify ordering of records for pattern
MEASURES RED.parking_space AS red_space -- results to be produced
, MATCH_NUMBER() AS match_num -- umptieth match
ALL ROWS PER MATCH –- all records in pattern or only the first
PATTERN (RED YELLOW BLUE) –- the pattern to locate
DEFINE –- row conditions to be used in pattern
RED AS RED.car_color = 'red', –- identify row with red car
YELLOW AS YELLOW.car_color = 'yellow', –- locate row with yellow car
BLUE AS BLUE.car_color = 'blue' –- record with blue car
) MR
ORDER
BY MR.red_space
, MR.parking_space
Up the ante – a little
• Suppose we also want to find blocks of cars
– For example: red-red-red-yellow-yellow-blue-blue
• And we accept white cars interspersed
between the colored ones
– So red-red-white-yellow-white-yellow-blue also
satisfies the pattern
• Lag/Lead solution quickly becomes unwieldy
Extending the pattern
match_recognize solution
SELECT * -- produces columns from parked_cars and from match_recognize
FROM parked_cars -- record set to find pattern in
MATCH_RECOGNIZE
(
ORDER BY parking_space -- specify ordering of records for pattern
MEASURES RED.parking_space AS red_space -- results to be produced
, MATCH_NUMBER() AS match_num -- umptieth match
ALL ROWS PER MATCH –- all records in pattern or only the first
PATTERN (RED+ WHITE* YELLOW+ WHITE* BLUE+)
DEFINE –- row conditions to be used in pattern
RED AS RED.car_color ='red', –- match on row with red car
YELLOW AS YELLOW.car_color ='yellow', –- match on yellow car
BLUE AS BLUE.car_color ='blue'–- match on blue car
WHITE AS WHITE.car_color ='white'–- match on white car
) MR
ORDER
BY MR.red_space
, MR.parking_space
Extending the pattern
match_recognize solution
SELECT * -- produces columns from parked_cars and from match_recognize
FROM parked_cars -- record set to find pattern in
MATCH_RECOGNIZE
(
ORDER BY parking_space -- specify ordering of records for pattern
MEASURES RED.parking_space AS red_space -- results to be produced
, MATCH_NUMBER() AS match_num -- umptieth match
ALL ROWS PER MATCH –- all records in pattern or only the first
PATTERN (RED+ WHITE* YELLOW+ WHITE* BLUE+)
DEFINE –- row conditions to be used in pattern
RED AS RED.car_color ='red', –- match on row with red car
YELLOW AS YELLOW.car_color ='yellow', –- match on yellow car
BLUE AS BLUE.car_color ='blue'–- match on blue car
WHITE AS WHITE.car_color ='white'–- match on white car
) MR
ORDER
BY MR.red_space
, MR.parking_space
Use a regular expression to
describe sought after pattern
• Supported operators for the pattern clause include:
– * for 0 or more iterations
– + for 1 or more iterations
– ? for 0 or 1 iterations
– { n } for exactly n iterations (n > 0)
– { n, } for n or more iterations (n >= 0)
– { n, m } for between n and m (inclusive) iterations (0 <= n <= m, 0 < m)
– { , m } for between 0 and m (inclusive) iterations (m > 0)
– reluctant qualifiers - *?, +?, ??, {n}?, {n,}?, { n, m }?, {,m}?
– | for alternation (OR)
– grouping using () parentheses
– exclusion using {- and -}
– empty pattern using ()
– ^ and $ for start and end of a partition
PATTERN (RED+ WHITE* YELLOW+ WHITE* BLUE+)
Elements of Match_Recognize
• FIRST, LAST, NEXT, PREV
• MATCH_NUMBER()
• CLASSIFIER()
• COUNT, SUM, AVG, MAX, MIN
• FINAL or RUNNING
• PER MATCH
– ALL ROWS or ONE ROW
• AFTER MATCH
– SKIP TO LAST, TO NEXT, FIRST, PAST LAST ROW
Did we ever hire three employees
in a row in the same job?
SELECT *
FROM EMP
MATCH_RECOGNIZE
(
ORDER BY hiredate
MEASURES SAME_JOB.hiredate AS hireday
, MATCH_NUMBER() AS match_num
ALL ROWS PER MATCH
PATTERN (SAME_JOB{3})
DEFINE
SAME_JOB AS SAME_JOB.job = FIRST(SAME_JOB.job)
) MR
Did we ever hire three employees
in a row in the same job?
SELECT *
FROM EMP
MATCH_RECOGNIZE
(
ORDER BY hiredate
MEASURES SAME_JOB.hiredate AS hireday
, MATCH_NUMBER() AS match_num
ALL ROWS PER MATCH
PATTERN (SAME_JOB{3})
DEFINE
SAME_JOB AS SAME_JOB.job = FIRST(SAME_JOB.job)
) MR
Conclusion
• Cool stuff
• Very fast
• Nifty tool for the SQL toolbox
• Useful for analysis of database activity
• Takes us beyond Analytical Functions for
advanced record interdependencies

More Related Content

PPTX
SQL for pattern matching (Oracle 12c)
PDF
Use Cases of Row Pattern Matching in Oracle 12c
PPTX
Row patternmatching12ctech14
PDF
The Magic of Window Functions in Postgres
 
PDF
Programming the SQL Way with Common Table Expressions
 
PPTX
1609oowemeadba lucasjellema-sqlpatternrecognition-160907125609
PPTX
Structured query language constraints
PDF
Matlab commands
SQL for pattern matching (Oracle 12c)
Use Cases of Row Pattern Matching in Oracle 12c
Row patternmatching12ctech14
The Magic of Window Functions in Postgres
 
Programming the SQL Way with Common Table Expressions
 
1609oowemeadba lucasjellema-sqlpatternrecognition-160907125609
Structured query language constraints
Matlab commands

What's hot (20)

PDF
SQL BUILT-IN FUNCTION
PPTX
Passing an Array to a Function (ICT Programming)
PPTX
RPG Sql regular expression
PPTX
Matlab ploting
PPTX
Unit 6. Arrays
PPTX
Matlab ch1 (3)
PDF
Programming Paradigms Which One Is The Best?
PPT
R for Statistical Computing
PPTX
Algebraic functions powerpoint
PPT
Array 31.8.2020 updated
PPTX
Python crush course
PDF
Sorting
PPTX
MATLAB - Arrays and Matrices
PPTX
DataFrame in Python Pandas
PDF
Matlab plotting
PPTX
PPT
358 33 powerpoint-slides_5-arrays_chapter-5
PPTX
PPT
C programming , array 2020
PDF
Matlabch01
SQL BUILT-IN FUNCTION
Passing an Array to a Function (ICT Programming)
RPG Sql regular expression
Matlab ploting
Unit 6. Arrays
Matlab ch1 (3)
Programming Paradigms Which One Is The Best?
R for Statistical Computing
Algebraic functions powerpoint
Array 31.8.2020 updated
Python crush course
Sorting
MATLAB - Arrays and Matrices
DataFrame in Python Pandas
Matlab plotting
358 33 powerpoint-slides_5-arrays_chapter-5
C programming , array 2020
Matlabch01
Ad

Viewers also liked (20)

PPTX
Ranges, ranges everywhere (Oracle SQL)
PPSX
Row Pattern Matching in Oracle Database 12c
PPTX
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
PPTX
Oracle OpenWorld 2016 Review - Focus on Data, BigData, Streaming Data, Machin...
PPTX
Introducing Node.js in an Oracle technology environment (including hands-on)
PPTX
What is the Oracle PaaS Cloud for Developers (Oracle Cloud Day, The Netherlan...
PPTX
Oracle OpenWorld 2016 Review - High Level Overview of major themes and grand ...
PPTX
Handson Oracle Management Cloud with Application Performance Monitoring and L...
PPTX
AMIS SIG - Introducing Apache Kafka - Scalable, reliable Event Bus & Message ...
PPTX
Comparing 30 MongoDB operations with Oracle SQL statements
PPTX
Introducing NoSQL and MongoDB to complement Relational Databases (AMIS SIG 14...
PDF
Row Pattern Matching in SQL:2016
PPTX
Systems on the edge - your stepping stones into Oracle Public PaaS Cloud - AM...
PPTX
The True State of the Oracle Public Cloud - Dutch Oracle Architects Platform ...
PPTX
Review Oracle OpenWorld 2015 - Overview, Main themes, Announcements and Future
PPTX
Introducing Oracle Real-Time Integration Business Insight
PPTX
Oracle Management Cloud - introduction, overview and getting started (AMIS, 2...
PDF
Introducing Kafka's Streams API
PDF
Pra latihan
PDF
ODTUG Technical Journal - The Reusability Test - Fusion Column July 2010
Ranges, ranges everywhere (Oracle SQL)
Row Pattern Matching in Oracle Database 12c
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
Oracle OpenWorld 2016 Review - Focus on Data, BigData, Streaming Data, Machin...
Introducing Node.js in an Oracle technology environment (including hands-on)
What is the Oracle PaaS Cloud for Developers (Oracle Cloud Day, The Netherlan...
Oracle OpenWorld 2016 Review - High Level Overview of major themes and grand ...
Handson Oracle Management Cloud with Application Performance Monitoring and L...
AMIS SIG - Introducing Apache Kafka - Scalable, reliable Event Bus & Message ...
Comparing 30 MongoDB operations with Oracle SQL statements
Introducing NoSQL and MongoDB to complement Relational Databases (AMIS SIG 14...
Row Pattern Matching in SQL:2016
Systems on the edge - your stepping stones into Oracle Public PaaS Cloud - AM...
The True State of the Oracle Public Cloud - Dutch Oracle Architects Platform ...
Review Oracle OpenWorld 2015 - Overview, Main themes, Announcements and Future
Introducing Oracle Real-Time Integration Business Insight
Oracle Management Cloud - introduction, overview and getting started (AMIS, 2...
Introducing Kafka's Streams API
Pra latihan
ODTUG Technical Journal - The Reusability Test - Fusion Column July 2010
Ad

Similar to Oracle Database 12c - Introducing SQL Pattern Recognition through MATCH_RECOGNIZE (Oracle OpenWorld 2016) (20)

PDF
SQL Pattern Matching – should I start using it?
PDF
TechEvent 2019: Uses of Row Pattern Matching; Kim Berg Hansen - Trivadis
PPTX
Uses of row pattern matching
DOCX
Predictive performance analysis using sql pattern matching
PPSX
Advanced row pattern matching
PPTX
http://boxinglatestnews.com
PDF
Oracle12c For Developers
PDF
Oracle12 for Developers - Oracle OpenWorld Preview AMIS
PPT
Advanced Sql Training
PDF
The ultimate-guide-to-sql
PPTX
Advanced functions in PL SQL
PPTX
Database Analysis, OLAP, Aggregate Functions
PPTX
Analysing Performance of Algorithmic SQL and PLSQL
PDF
Database solution by m.moses wills
PDF
Database solution by m.moses wills
PPTX
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
ODP
Oracle SQL Advanced
PDF
Geek Sync | Breaking Bad Habits: Solutions for Common Query Antipatterns - Je...
PPTX
SQL.pptx
PPT
Analytics ioug 2011
SQL Pattern Matching – should I start using it?
TechEvent 2019: Uses of Row Pattern Matching; Kim Berg Hansen - Trivadis
Uses of row pattern matching
Predictive performance analysis using sql pattern matching
Advanced row pattern matching
http://boxinglatestnews.com
Oracle12c For Developers
Oracle12 for Developers - Oracle OpenWorld Preview AMIS
Advanced Sql Training
The ultimate-guide-to-sql
Advanced functions in PL SQL
Database Analysis, OLAP, Aggregate Functions
Analysing Performance of Algorithmic SQL and PLSQL
Database solution by m.moses wills
Database solution by m.moses wills
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
Oracle SQL Advanced
Geek Sync | Breaking Bad Habits: Solutions for Common Query Antipatterns - Je...
SQL.pptx
Analytics ioug 2011

More from Lucas Jellema (20)

PPTX
Introduction to web application development with Vue (for absolute beginners)...
PPTX
Making the Shift Left - Bringing Ops to Dev before bringing applications to p...
PPTX
Lightweight coding in powerful Cloud Development Environments (DigitalXchange...
PPTX
Apache Superset - open source data exploration and visualization (Conclusion ...
PPTX
CONNECTING THE REAL WORLD TO ENTERPRISE IT – HOW IoT DRIVES OUR ENERGY TRANSI...
PPTX
Help me move away from Oracle - or not?! (Oracle Community Tour EMEA - LVOUG...
PPTX
Op je vingers tellen... tot 1000!
PPTX
IoT - from prototype to enterprise platform (DigitalXchange 2022)
PPTX
Who Wants to Become an IT Architect-A Look at the Bigger Picture - DigitalXch...
PPTX
Steampipe - use SQL to retrieve data from cloud, platforms and files (Code Ca...
PPTX
Automation of Software Engineering with OCI DevOps Build and Deployment Pipel...
PPTX
Introducing Dapr.io - the open source personal assistant to microservices and...
PPTX
How and Why you can and should Participate in Open Source Projects (AMIS, Sof...
PPTX
Microservices, Apache Kafka, Node, Dapr and more - Part Two (Fontys Hogeschoo...
PPTX
Microservices, Node, Dapr and more - Part One (Fontys Hogeschool, Spring 2022)
PPTX
6Reinventing Oracle Systems in a Cloudy World (RMOUG Trainingdays, February 2...
PPTX
Help me move away from Oracle! (RMOUG Training Days 2022, February 2022)
PPTX
Tech Talks 101 - DevOps (jan 2022)
PPTX
Conclusion Code Cafe - Microcks for Mocking and Testing Async APIs (January 2...
PPTX
Cloud Native Application Development - build fast, low TCO, scalable & agile ...
Introduction to web application development with Vue (for absolute beginners)...
Making the Shift Left - Bringing Ops to Dev before bringing applications to p...
Lightweight coding in powerful Cloud Development Environments (DigitalXchange...
Apache Superset - open source data exploration and visualization (Conclusion ...
CONNECTING THE REAL WORLD TO ENTERPRISE IT – HOW IoT DRIVES OUR ENERGY TRANSI...
Help me move away from Oracle - or not?! (Oracle Community Tour EMEA - LVOUG...
Op je vingers tellen... tot 1000!
IoT - from prototype to enterprise platform (DigitalXchange 2022)
Who Wants to Become an IT Architect-A Look at the Bigger Picture - DigitalXch...
Steampipe - use SQL to retrieve data from cloud, platforms and files (Code Ca...
Automation of Software Engineering with OCI DevOps Build and Deployment Pipel...
Introducing Dapr.io - the open source personal assistant to microservices and...
How and Why you can and should Participate in Open Source Projects (AMIS, Sof...
Microservices, Apache Kafka, Node, Dapr and more - Part Two (Fontys Hogeschoo...
Microservices, Node, Dapr and more - Part One (Fontys Hogeschool, Spring 2022)
6Reinventing Oracle Systems in a Cloudy World (RMOUG Trainingdays, February 2...
Help me move away from Oracle! (RMOUG Training Days 2022, February 2022)
Tech Talks 101 - DevOps (jan 2022)
Conclusion Code Cafe - Microcks for Mocking and Testing Async APIs (January 2...
Cloud Native Application Development - build fast, low TCO, scalable & agile ...

Recently uploaded (20)

PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PPTX
VVF-Customer-Presentation2025-Ver1.9.pptx
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PPTX
L1 - Introduction to python Backend.pptx
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PDF
top salesforce developer skills in 2025.pdf
PDF
Nekopoi APK 2025 free lastest update
PPTX
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
PPTX
Introduction to Artificial Intelligence
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
medical staffing services at VALiNTRY
PPTX
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Navsoft: AI-Powered Business Solutions & Custom Software Development
Odoo POS Development Services by CandidRoot Solutions
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
VVF-Customer-Presentation2025-Ver1.9.pptx
ISO 45001 Occupational Health and Safety Management System
How to Choose the Right IT Partner for Your Business in Malaysia
L1 - Introduction to python Backend.pptx
ManageIQ - Sprint 268 Review - Slide Deck
top salesforce developer skills in 2025.pdf
Nekopoi APK 2025 free lastest update
Agentic AI Use Case- Contract Lifecycle Management (CLM).pptx
Introduction to Artificial Intelligence
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Softaken Excel to vCard Converter Software.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PTS Company Brochure 2025 (1).pdf.......
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
medical staffing services at VALiNTRY
CHAPTER 12 - CYBER SECURITY AND FUTURE SKILLS (1) (1).pptx
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...

Oracle Database 12c - Introducing SQL Pattern Recognition through MATCH_RECOGNIZE (Oracle OpenWorld 2016)

  • 1. SQL Pattern Recognition (boldly go beyond analytical) from 12.1 Lucas Jellema speaks at conferences and user group events, writes blog articles (on the AMIS Technology Blog) and has published two books with Oracle Press (on Oracle SOA Suite). His interests range from client side UI and JavaScript through integration middleware to Database development and platform design. Creative usages of SQL and PL/SQL are among his favorite pastimes. In his day time job, Lucas is CTO and architecture consultant at AMIS in The Netherlands and is affiliated with the Dutch Oracle User Group (OGh). Lucas Jellema @lucasjellema Oracle ACE Director
  • 3. Pattern Matching • Discover special patterns in [potentially pretty big] data sets • Crucial requirement: records have to be ordered in some way – Frequently by time [of observation] – Could be by the location along some axis
  • 4. Presenting: Modern Art • “Who’s afraid of red, yellow and blue” – Barnett Newman, Stedelijk Museum, Amsterdam
  • 5. Find art in the car park • Find if we have cars parked according to the red-yellow-blue pattern of the painting Parking Space Car color
  • 6. Analytical Functions • Solution with Lag or Lead • With LEAD it is easy to compare a row with its successor(s) – As long as the pattern is fixed, LEAD will suffice with look_ahead_cars as ( SELECT c.* -- for each car, find next and next after that , lead(car_color,1) over (order by parking_space) next_color , lead(car_color,2) over (order by parking_space) sec_nxt_color FROM parked_cars c ) select parking_space from look_ahead_cars where car_color ='red' –- for each red car and next_color ='yellow' -- check if next is yellow and sec_nxt_color='blue' –- and the one after that is blue
  • 7. Match Recognize • New operator in 12c: MATCH_RECOGNIZE – Specifically provided for pattern matching – Pretty fast and very versatile
  • 8. Match Recognize • New operator in 12c: MATCH_RECOGNIZE – Specifically provided for pattern matching SELECT * -- produces columns from parked_cars and from match_recognize FROM parked_cars -- record set to find pattern in MATCH_RECOGNIZE ( ORDER BY parking_space -- specify ordering of records for pattern MEASURES RED.parking_space AS red_space -- results to be produced , MATCH_NUMBER() AS match_num -- umptieth match ALL ROWS PER MATCH –- all records in pattern or only the first PATTERN (RED YELLOW BLUE) –- the pattern to locate DEFINE –- row conditions to be used in pattern RED AS RED.car_color ='red', –- match on row with red car YELLOW AS YELLOW.car_color ='yellow', –- match on yellow car BLUE AS BLUE.car_color ='blue‘–- match on blue car ) MR ORDER BY MR.red_space , MR.parking_space
  • 9. Match Recognize • New operator in 12c: MATCH_RECOGNIZE – Specifically provided for pattern matching SELECT * -- produces columns from parked_cars and from match_recognize FROM parked_cars -- record set to find pattern in MATCH_RECOGNIZE ( ORDER BY parking_space -- specify ordering of records for pattern MEASURES RED.parking_space AS red_space -- results to be produced , MATCH_NUMBER() AS match_num -- umptieth match ALL ROWS PER MATCH –- all records in pattern or only the first PATTERN (RED YELLOW BLUE) –- the pattern to locate DEFINE –- row conditions to be used in pattern RED AS RED.car_color = 'red', –- identify row with red car YELLOW AS YELLOW.car_color = 'yellow', –- locate row with yellow car BLUE AS BLUE.car_color = 'blue' –- record with blue car ) MR ORDER BY MR.red_space , MR.parking_space
  • 10. Up the ante – a little • Suppose we also want to find blocks of cars – For example: red-red-red-yellow-yellow-blue-blue • And we accept white cars interspersed between the colored ones – So red-red-white-yellow-white-yellow-blue also satisfies the pattern • Lag/Lead solution quickly becomes unwieldy
  • 11. Extending the pattern match_recognize solution SELECT * -- produces columns from parked_cars and from match_recognize FROM parked_cars -- record set to find pattern in MATCH_RECOGNIZE ( ORDER BY parking_space -- specify ordering of records for pattern MEASURES RED.parking_space AS red_space -- results to be produced , MATCH_NUMBER() AS match_num -- umptieth match ALL ROWS PER MATCH –- all records in pattern or only the first PATTERN (RED+ WHITE* YELLOW+ WHITE* BLUE+) DEFINE –- row conditions to be used in pattern RED AS RED.car_color ='red', –- match on row with red car YELLOW AS YELLOW.car_color ='yellow', –- match on yellow car BLUE AS BLUE.car_color ='blue'–- match on blue car WHITE AS WHITE.car_color ='white'–- match on white car ) MR ORDER BY MR.red_space , MR.parking_space
  • 12. Extending the pattern match_recognize solution SELECT * -- produces columns from parked_cars and from match_recognize FROM parked_cars -- record set to find pattern in MATCH_RECOGNIZE ( ORDER BY parking_space -- specify ordering of records for pattern MEASURES RED.parking_space AS red_space -- results to be produced , MATCH_NUMBER() AS match_num -- umptieth match ALL ROWS PER MATCH –- all records in pattern or only the first PATTERN (RED+ WHITE* YELLOW+ WHITE* BLUE+) DEFINE –- row conditions to be used in pattern RED AS RED.car_color ='red', –- match on row with red car YELLOW AS YELLOW.car_color ='yellow', –- match on yellow car BLUE AS BLUE.car_color ='blue'–- match on blue car WHITE AS WHITE.car_color ='white'–- match on white car ) MR ORDER BY MR.red_space , MR.parking_space
  • 13. Use a regular expression to describe sought after pattern • Supported operators for the pattern clause include: – * for 0 or more iterations – + for 1 or more iterations – ? for 0 or 1 iterations – { n } for exactly n iterations (n > 0) – { n, } for n or more iterations (n >= 0) – { n, m } for between n and m (inclusive) iterations (0 <= n <= m, 0 < m) – { , m } for between 0 and m (inclusive) iterations (m > 0) – reluctant qualifiers - *?, +?, ??, {n}?, {n,}?, { n, m }?, {,m}? – | for alternation (OR) – grouping using () parentheses – exclusion using {- and -} – empty pattern using () – ^ and $ for start and end of a partition PATTERN (RED+ WHITE* YELLOW+ WHITE* BLUE+)
  • 14. Elements of Match_Recognize • FIRST, LAST, NEXT, PREV • MATCH_NUMBER() • CLASSIFIER() • COUNT, SUM, AVG, MAX, MIN • FINAL or RUNNING • PER MATCH – ALL ROWS or ONE ROW • AFTER MATCH – SKIP TO LAST, TO NEXT, FIRST, PAST LAST ROW
  • 15. Did we ever hire three employees in a row in the same job? SELECT * FROM EMP MATCH_RECOGNIZE ( ORDER BY hiredate MEASURES SAME_JOB.hiredate AS hireday , MATCH_NUMBER() AS match_num ALL ROWS PER MATCH PATTERN (SAME_JOB{3}) DEFINE SAME_JOB AS SAME_JOB.job = FIRST(SAME_JOB.job) ) MR
  • 16. Did we ever hire three employees in a row in the same job? SELECT * FROM EMP MATCH_RECOGNIZE ( ORDER BY hiredate MEASURES SAME_JOB.hiredate AS hireday , MATCH_NUMBER() AS match_num ALL ROWS PER MATCH PATTERN (SAME_JOB{3}) DEFINE SAME_JOB AS SAME_JOB.job = FIRST(SAME_JOB.job) ) MR
  • 17. Conclusion • Cool stuff • Very fast • Nifty tool for the SQL toolbox • Useful for analysis of database activity • Takes us beyond Analytical Functions for advanced record interdependencies