16
BASEL BERN BRUGG DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENF HAMBURG KOPENHAGEN LAUSANNE MÜNCHEN STUTTGART WIEN ZÜRICH Use Cases of Row Pattern Matching Webinar on MATCH_RECOGNIZE as a versatile SQL tool Kim Berg Hansen Senior Consultant

Use Cases of Row Pattern Matching in Oracle 12c

  • Upload
    gerger

  • View
    214

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Use Cases of Row Pattern Matching in Oracle 12c

BASEL BERN BRUGG DÜSSELDORF FRANKFURT A.M. FREIBURG I.BR. GENF

HAMBURG KOPENHAGEN LAUSANNE MÜNCHEN STUTTGART WIEN ZÜRICH

Use Cases of Row Pattern MatchingWebinar on MATCH_RECOGNIZE as a versatile SQL tool

Kim Berg HansenSenior Consultant

Page 2: Use Cases of Row Pattern Matching in Oracle 12c

• Danish geek

• SQL & PL/SQL developer since 2000

• Developer at Trivadis AG since 2016

http://www.trivadis.dk

• Oracle Certified Expert in SQL

• Oracle ACE

• Blogger at http://www.kibeha.dk

• SQL quizmaster at

http://plsqlchallenge.oracle.com

• Likes to cook

• Reads sci-fi

• Chairman of local chapter of

Danish Beer Enthusiasts

About me

Use Cases of Row Pattern Matching2 2/23/2016

Page 3: Use Cases of Row Pattern Matching in Oracle 12c

About Trivadis

© Trivadis – The Company3 2/23/2016

Trivadis is a market leader in IT consulting, system integration, solution engineering

and the provision of IT services focusing on and

technologies in Switzerland, Germany, Austria and Denmark.

We offer our services in the following strategic business fields:

Trivadis Services takes over the interacting operation of your IT systems.

O P E R A T I O N

Page 4: Use Cases of Row Pattern Matching in Oracle 12c

COPENHAGEN

MUNICH

LAUSANNE

BERN

ZURICH

BRUGG

GENEVA

HAMBURG

DÜSSELDORF

FRANKFURT

STUTTGART

FREIBURG

BASEL

VIENNA

With over 600 specialists and IT experts in your region

© Trivadis – The Company4 2/23/2016

14 Trivadis branches and more than

600 employees

260 Service Level Agreements

Over 4,000 training participants

Research and development budget:

EUR 5.0 million

Financially self-supporting and

sustainably profitable

Experience from more than 1,900

projects per year at over 800

customers

Page 5: Use Cases of Row Pattern Matching in Oracle 12c

2/23/2016 © Trivadis – The Company5

Technology on its own won't help you.You need to know how to use it properly.

Page 6: Use Cases of Row Pattern Matching in Oracle 12c

Agenda

Use Cases of Row Pattern Matching6 2/23/2016

Brief syntax overview

1. Grouping contiguous sequences

2. Grouping by time range (I)

3. Grouping by time range (II)

4. Merge date ranges

5. Group with limit on aggregate (bin fitting I)

6. Fill bins to near equal size (bin fitting II)

7. Network hickups

8. Tablespace growth spurts

9. Hierarchical child count

Brief summary

Q & A

Page 7: Use Cases of Row Pattern Matching in Oracle 12c

Use Cases of Row Pattern Matching7 2/23/2016

Brief syntax overview

Page 8: Use Cases of Row Pattern Matching in Oracle 12c

What‘s it look like

Think in Patterns8 2/23/2016

SELECT *

FROM Ticker MATCH_RECOGNIZE (

PARTITION BY symbol

ORDER BY tstamp

MEASURES STRT.tstamp AS start_tstamp,

FINAL LAST(DOWN.tstamp) AS bottom_tstamp,

FINAL LAST(UP.tstamp) AS end_tstamp,

MATCH_NUMBER() AS match_num,

CLASSIFIER() AS var_match

ALL ROWS PER MATCH

AFTER MATCH SKIP TO LAST UP

PATTERN (STRT DOWN+ UP+)

DEFINE

DOWN AS DOWN.price < PREV(DOWN.price),

UP AS UP.price > PREV(UP.price)

) MR

ORDER BY MR.symbol, MR.match_num, MR.tstamp

Example from Data Warehousing Guide chapter on SQL For Pattern Matching

Page 9: Use Cases of Row Pattern Matching in Oracle 12c

The result

Think in Patterns9 2/23/2016

SYMBOL TSTAMP START_TST BOTTOM_TS END_TSTAM MATCH_NUM VAR_ PRICE

---------- --------- --------- --------- --------- ---------- ---- ----------

ACME 05-APR-11 05-APR-11 06-APR-11 10-APR-11 1 STRT 25

ACME 06-APR-11 05-APR-11 06-APR-11 10-APR-11 1 DOWN 12

ACME 07-APR-11 05-APR-11 06-APR-11 10-APR-11 1 UP 15

ACME 08-APR-11 05-APR-11 06-APR-11 10-APR-11 1 UP 20

ACME 09-APR-11 05-APR-11 06-APR-11 10-APR-11 1 UP 24

ACME 10-APR-11 05-APR-11 06-APR-11 10-APR-11 1 UP 25

ACME 10-APR-11 10-APR-11 12-APR-11 13-APR-11 2 STRT 25

ACME 11-APR-11 10-APR-11 12-APR-11 13-APR-11 2 DOWN 19

ACME 12-APR-11 10-APR-11 12-APR-11 13-APR-11 2 DOWN 15

ACME 13-APR-11 10-APR-11 12-APR-11 13-APR-11 2 UP 25

ACME 14-APR-11 14-APR-11 16-APR-11 18-APR-11 3 STRT 25

ACME 15-APR-11 14-APR-11 16-APR-11 18-APR-11 3 DOWN 14

ACME 16-APR-11 14-APR-11 16-APR-11 18-APR-11 3 DOWN 12

ACME 17-APR-11 14-APR-11 16-APR-11 18-APR-11 3 UP 14

ACME 18-APR-11 14-APR-11 16-APR-11 18-APR-11 3 UP 24

The output of the example in the previous slide

Page 10: Use Cases of Row Pattern Matching in Oracle 12c

Elements

Use Cases of Row Pattern Matching10 2/23/2016

PARTITION BY – like analytics split data to work on one partition at a time

ORDER BY – in which order shall rows be tested whether they match the pattern

MEASURES – the information we want returned from the match

ALL ROWS / ONE ROW PER MATCH – return aggregate or detailed info for match

AFTER MATCH SKIP … – when match found, where to start looking for new match

PATTERN – regexp like syntax of pattern of defined row classifiers to match

SUBSET – „union“ a set of classifications into one classification variable

DEFINE – definition of classification of rows

FIRST, LAST, PREV, NEXT – navigational functions

CLASSIFIER(), MATCH_NUMBER() – identification functions

Page 11: Use Cases of Row Pattern Matching in Oracle 12c

Use Cases of Row Pattern Matching11 2/23/2016

9 use cases

Page 12: Use Cases of Row Pattern Matching in Oracle 12c

9 Demos

Use Cases of Row Pattern Matching12 2/23/2016

Page 13: Use Cases of Row Pattern Matching in Oracle 12c

Use Cases of Row Pattern Matching13 2/23/2016

Brief summary

Page 14: Use Cases of Row Pattern Matching in Oracle 12c

MATCH_RECOGNIZE - A “swiss army knife” tool

Use Cases of Row Pattern Matching14 2/23/2016

Brilliant when applied “BI style” like stock ticker analysis examples

But applicable to many other cases too

When you have some problem crossing row boundaries and feel you have to

“stretch” even the capabilities of analytics, try a pattern based approach:

– Rephrase (in natural language) your requirements in terms of what classifies the

rows you are looking for

– Turn that into pattern matching syntax classifying individual rows in DEFINE and

how the classified rows should appear in PATTERN

As with analytics, it might feel daunting at first, but once you start using pattern

matching, it will become just another tool in your SQL toolbox

Page 15: Use Cases of Row Pattern Matching in Oracle 12c

Use Cases of Row Pattern Matching15 2/23/2016

Further information

Demo scripts used in this webinar

– http://bit.ly/patternmatchsamples

Follow Stew Ashton’s blog posts on the topic

– https://stewashton.wordpress.com/category/match_recognize/

Documentation in Data Warehousing Guide

– http://docs.oracle.com/database/121/DWHSG/pattern.htm

Page 16: Use Cases of Row Pattern Matching in Oracle 12c

Q & AKim Berg Hansen

Senior Consultant

[email protected]

2/23/2016 Use Cases of Row Pattern Matching16