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
Ad

Recommended

PPTX
SQL for pattern matching (Oracle 12c)
Logan Palanisamy
 
PDF
Use Cases of Row Pattern Matching in Oracle 12c
Gerger
 
PPTX
Row patternmatching12ctech14
stewashton
 
PDF
The Magic of Window Functions in Postgres
EDB
 
PDF
Programming the SQL Way with Common Table Expressions
EDB
 
PPTX
1609oowemeadba lucasjellema-sqlpatternrecognition-160907125609
Getting value from IoT, Integration and Data Analytics
 
PPTX
Structured query language constraints
Vineeta Garg
 
PDF
Matlab commands
Tarun Gehlot
 
PDF
SQL BUILT-IN FUNCTION
Arun Sial
 
PPTX
Passing an Array to a Function (ICT Programming)
Fatima Kate Tanay
 
PPTX
RPG Sql regular expression
Jose Perez
 
PPTX
Matlab ploting
Ameen San
 
PPTX
Unit 6. Arrays
Ashim Lamichhane
 
PPTX
Matlab ch1 (3)
mohsinggg
 
PDF
Programming Paradigms Which One Is The Best?
Netguru
 
PPT
R for Statistical Computing
Mohammed El Rafie Tarabay
 
PPTX
Algebraic functions powerpoint
Caron White
 
PPT
Array 31.8.2020 updated
vrgokila
 
PPTX
Python crush course
Mohammed El Rafie Tarabay
 
PDF
Sorting
Zaid Shabbir
 
PPTX
MATLAB - Arrays and Matrices
Shameer Ahmed Koya
 
PPTX
DataFrame in Python Pandas
Sangita Panchal
 
PDF
Matlab plotting
pramodkumar1804
 
PPTX
SQL
Siti Ismail
 
PPT
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
PPTX
Python
Sangita Panchal
 
PPT
C programming , array 2020
Osama Ghandour Geris
 
PDF
Matlabch01
Mohammad Ayyash
 
PPTX
Ranges, ranges everywhere (Oracle SQL)
Stew Ashton
 
PPSX
Row Pattern Matching in Oracle Database 12c
Stew Ashton
 

More Related Content

What's hot (20)

PDF
SQL BUILT-IN FUNCTION
Arun Sial
 
PPTX
Passing an Array to a Function (ICT Programming)
Fatima Kate Tanay
 
PPTX
RPG Sql regular expression
Jose Perez
 
PPTX
Matlab ploting
Ameen San
 
PPTX
Unit 6. Arrays
Ashim Lamichhane
 
PPTX
Matlab ch1 (3)
mohsinggg
 
PDF
Programming Paradigms Which One Is The Best?
Netguru
 
PPT
R for Statistical Computing
Mohammed El Rafie Tarabay
 
PPTX
Algebraic functions powerpoint
Caron White
 
PPT
Array 31.8.2020 updated
vrgokila
 
PPTX
Python crush course
Mohammed El Rafie Tarabay
 
PDF
Sorting
Zaid Shabbir
 
PPTX
MATLAB - Arrays and Matrices
Shameer Ahmed Koya
 
PPTX
DataFrame in Python Pandas
Sangita Panchal
 
PDF
Matlab plotting
pramodkumar1804
 
PPTX
SQL
Siti Ismail
 
PPT
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
PPTX
Python
Sangita Panchal
 
PPT
C programming , array 2020
Osama Ghandour Geris
 
PDF
Matlabch01
Mohammad Ayyash
 
SQL BUILT-IN FUNCTION
Arun Sial
 
Passing an Array to a Function (ICT Programming)
Fatima Kate Tanay
 
RPG Sql regular expression
Jose Perez
 
Matlab ploting
Ameen San
 
Unit 6. Arrays
Ashim Lamichhane
 
Matlab ch1 (3)
mohsinggg
 
Programming Paradigms Which One Is The Best?
Netguru
 
R for Statistical Computing
Mohammed El Rafie Tarabay
 
Algebraic functions powerpoint
Caron White
 
Array 31.8.2020 updated
vrgokila
 
Python crush course
Mohammed El Rafie Tarabay
 
Sorting
Zaid Shabbir
 
MATLAB - Arrays and Matrices
Shameer Ahmed Koya
 
DataFrame in Python Pandas
Sangita Panchal
 
Matlab plotting
pramodkumar1804
 
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
C programming , array 2020
Osama Ghandour Geris
 
Matlabch01
Mohammad Ayyash
 

Viewers also liked (20)

PPTX
Ranges, ranges everywhere (Oracle SQL)
Stew Ashton
 
PPSX
Row Pattern Matching in Oracle Database 12c
Stew Ashton
 
PPTX
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
stewashton
 
PPTX
Oracle OpenWorld 2016 Review - Focus on Data, BigData, Streaming Data, Machin...
Lucas Jellema
 
PPTX
Introducing Node.js in an Oracle technology environment (including hands-on)
Lucas Jellema
 
PPTX
What is the Oracle PaaS Cloud for Developers (Oracle Cloud Day, The Netherlan...
Lucas Jellema
 
PPTX
Oracle OpenWorld 2016 Review - High Level Overview of major themes and grand ...
Lucas Jellema
 
PPTX
Handson Oracle Management Cloud with Application Performance Monitoring and L...
Lucas Jellema
 
PPTX
AMIS SIG - Introducing Apache Kafka - Scalable, reliable Event Bus & Message ...
Lucas Jellema
 
PPTX
Comparing 30 MongoDB operations with Oracle SQL statements
Lucas Jellema
 
PPTX
Introducing NoSQL and MongoDB to complement Relational Databases (AMIS SIG 14...
Lucas Jellema
 
PDF
Row Pattern Matching in SQL:2016
Markus Winand
 
PPTX
Systems on the edge - your stepping stones into Oracle Public PaaS Cloud - AM...
Lucas Jellema
 
PPTX
The True State of the Oracle Public Cloud - Dutch Oracle Architects Platform ...
Lucas Jellema
 
PPTX
Review Oracle OpenWorld 2015 - Overview, Main themes, Announcements and Future
Lucas Jellema
 
PPTX
Introducing Oracle Real-Time Integration Business Insight
Lucas Jellema
 
PPTX
Oracle Management Cloud - introduction, overview and getting started (AMIS, 2...
Lucas Jellema
 
PDF
Introducing Kafka's Streams API
confluent
 
PDF
Pra latihan
rafipolman
 
PDF
ODTUG Technical Journal - The Reusability Test - Fusion Column July 2010
Lucas Jellema
 
Ranges, ranges everywhere (Oracle SQL)
Stew Ashton
 
Row Pattern Matching in Oracle Database 12c
Stew Ashton
 
Row Pattern Matching 12c MATCH_RECOGNIZE OOW14
stewashton
 
Oracle OpenWorld 2016 Review - Focus on Data, BigData, Streaming Data, Machin...
Lucas Jellema
 
Introducing Node.js in an Oracle technology environment (including hands-on)
Lucas Jellema
 
What is the Oracle PaaS Cloud for Developers (Oracle Cloud Day, The Netherlan...
Lucas Jellema
 
Oracle OpenWorld 2016 Review - High Level Overview of major themes and grand ...
Lucas Jellema
 
Handson Oracle Management Cloud with Application Performance Monitoring and L...
Lucas Jellema
 
AMIS SIG - Introducing Apache Kafka - Scalable, reliable Event Bus & Message ...
Lucas Jellema
 
Comparing 30 MongoDB operations with Oracle SQL statements
Lucas Jellema
 
Introducing NoSQL and MongoDB to complement Relational Databases (AMIS SIG 14...
Lucas Jellema
 
Row Pattern Matching in SQL:2016
Markus Winand
 
Systems on the edge - your stepping stones into Oracle Public PaaS Cloud - AM...
Lucas Jellema
 
The True State of the Oracle Public Cloud - Dutch Oracle Architects Platform ...
Lucas Jellema
 
Review Oracle OpenWorld 2015 - Overview, Main themes, Announcements and Future
Lucas Jellema
 
Introducing Oracle Real-Time Integration Business Insight
Lucas Jellema
 
Oracle Management Cloud - introduction, overview and getting started (AMIS, 2...
Lucas Jellema
 
Introducing Kafka's Streams API
confluent
 
Pra latihan
rafipolman
 
ODTUG Technical Journal - The Reusability Test - Fusion Column July 2010
Lucas Jellema
 
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?
Andrej Pashchenko
 
PDF
TechEvent 2019: Uses of Row Pattern Matching; Kim Berg Hansen - Trivadis
Trivadis
 
PPTX
Uses of row pattern matching
Kim Berg Hansen
 
DOCX
Predictive performance analysis using sql pattern matching
Horia Berca
 
PPSX
Advanced row pattern matching
Stew Ashton
 
PPTX
http://boxinglatestnews.com
David Lopez
 
PDF
Oracle12c For Developers
Alex Nuijten
 
PDF
Oracle12 for Developers - Oracle OpenWorld Preview AMIS
Getting value from IoT, Integration and Data Analytics
 
PPT
Advanced Sql Training
bixxman
 
PDF
The ultimate-guide-to-sql
McNamaraChiwaye
 
PPTX
Advanced functions in PL SQL
Hosein Zare
 
PPTX
Database Analysis, OLAP, Aggregate Functions
Saifur Rahman
 
PPTX
Analysing Performance of Algorithmic SQL and PLSQL.pptx
Brendan Furey
 
PDF
Database solution by m.moses wills
Moses Mwebaze
 
PDF
Database solution by m.moses wills
Moses Mwebaze
 
PPTX
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
ssuser9dddf7
 
ODP
Oracle SQL Advanced
Dhananjay Goel
 
PDF
Geek Sync | Breaking Bad Habits: Solutions for Common Query Antipatterns - Je...
IDERA Software
 
PPTX
SQL.pptx
AmitDas125851
 
PPT
Analytics ioug 2011
carldudley
 
SQL Pattern Matching – should I start using it?
Andrej Pashchenko
 
TechEvent 2019: Uses of Row Pattern Matching; Kim Berg Hansen - Trivadis
Trivadis
 
Uses of row pattern matching
Kim Berg Hansen
 
Predictive performance analysis using sql pattern matching
Horia Berca
 
Advanced row pattern matching
Stew Ashton
 
http://boxinglatestnews.com
David Lopez
 
Oracle12c For Developers
Alex Nuijten
 
Oracle12 for Developers - Oracle OpenWorld Preview AMIS
Getting value from IoT, Integration and Data Analytics
 
Advanced Sql Training
bixxman
 
The ultimate-guide-to-sql
McNamaraChiwaye
 
Advanced functions in PL SQL
Hosein Zare
 
Database Analysis, OLAP, Aggregate Functions
Saifur Rahman
 
Analysing Performance of Algorithmic SQL and PLSQL.pptx
Brendan Furey
 
Database solution by m.moses wills
Moses Mwebaze
 
Database solution by m.moses wills
Moses Mwebaze
 
Lecture8-SQL-PartI-Jan30-2018 test Lecture8-SQL-PartI-Jan30-2018 test
ssuser9dddf7
 
Oracle SQL Advanced
Dhananjay Goel
 
Geek Sync | Breaking Bad Habits: Solutions for Common Query Antipatterns - Je...
IDERA Software
 
SQL.pptx
AmitDas125851
 
Analytics ioug 2011
carldudley
 
Ad

More from Lucas Jellema (20)

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

Recently uploaded (20)

PPTX
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
PDF
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
 
PDF
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
PPTX
IObit Driver Booster Pro 12 Crack Latest Version Download
pcprocore
 
PPTX
AI for PV: Development and Governance for a Regulated Industry
Biologit
 
PDF
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
PDF
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
PDF
University Campus Navigation for All - Peak of Data & AI
Safe Software
 
PDF
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
PPTX
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
 
PDF
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
mary rojas
 
PPTX
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
PDF
Complete WordPress Programming Guidance Book
Shabista Imam
 
PDF
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
PDF
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
 
PDF
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
 
PPTX
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
PDF
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
 
PPTX
Advance Doctor Appointment Booking App With Online Payment
AxisTechnolabs
 
PDF
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 
ERP Systems in the UAE: Driving Business Transformation with Smart Solutions
dheeodoo
 
Humans vs AI Call Agents - Qcall.ai's Special Report
Udit Goenka
 
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
IObit Driver Booster Pro 12 Crack Latest Version Download
pcprocore
 
AI for PV: Development and Governance for a Regulated Industry
Biologit
 
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
University Campus Navigation for All - Peak of Data & AI
Safe Software
 
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
 
Why Every Growing Business Needs a Staff Augmentation Company IN USA.pdf
mary rojas
 
Threat Modeling a Batch Job Framework - Teri Radichel - AWS re:Inforce 2025
2nd Sight Lab
 
Complete WordPress Programming Guidance Book
Shabista Imam
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
 
From Data Preparation to Inference: How Alluxio Speeds Up AI
Alluxio, Inc.
 
IDM Crack with Internet Download Manager 6.42 [Latest 2025]
HyperPc soft
 
Digital Transformation: Automating the Placement of Medical Interns
Safe Software
 
Advance Doctor Appointment Booking App With Online Payment
AxisTechnolabs
 
Canva Pro Crack Free Download 2025-FREE LATEST
grete1122g
 

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