Slide 1

Slide 1 text

Your SQL Office Hours begins soon… SQL Pattern Matching + SQL Macros Chris Saxon @ChrisRSaxon & @SQLDaily https://www.youtube.com/c/TheMagicofSQL https://blogs.oracle.com/sql

Slide 2

Slide 2 text

The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, timing, and pricing of any features or functionality described for Oracle’s products may change and remains at the sole discretion of Oracle Corporation. Statements in this presentation relating to Oracle’s future plans, expectations, beliefs, intentions and prospects are “forward-looking statements” and are subject to material risks and uncertainties. A detailed discussion of these factors and other risks that affect our business is contained in Oracle’s Securities and Exchange Commission (SEC) filings, including our most recent reports on Form 10-K and Form 10-Q under the heading “Risk Factors.” These filings are available on the SEC’s website or on Oracle’s website at http://www.oracle.com/investor. All information in this presentation is current as of September 2020 and Oracle undertakes no duty to update any statement in light of new information or future events. Safe Harbor Copyright © 2020 Oracle and/or its affiliates.

Slide 3

Slide 3 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | Welcome to Ask TOM Office Hours!

Slide 4

Slide 4 text

Am I running every day? Ryan McGuire / Gratisography

Slide 5

Slide 5 text

RUN_DATE TIME_IN_S DISTANCE_IN_KM 01 Apr 2020 310 1 02 Apr 2020 1,600 5 03 Apr 2020 3,580 11 06 Apr 2020 1,550 5 07 Apr 2020 300 1 10 Apr 2020 280 1 13 Apr 2020 1,530 5 14 Apr 2020 295 1 15 Apr 2020 292 1

Slide 6

Slide 6 text

RUN_DATE TIME_IN_S DISTANCE_IN_KM 01 Apr 2020 310 1 02 Apr 2020 1,600 5 03 Apr 2020 3,580 11 06 Apr 2020 1,550 5 07 Apr 2020 300 1 10 Apr 2020 280 1 13 Apr 2020 1,530 5 14 Apr 2020 295 1 15 Apr 2020 292 1 #1 #3 #2 #4

Slide 7

Slide 7 text

How I know if rows are consecutive?

Slide 8

Slide 8 text

current value = previous value + 1

Slide 9

Slide 9 text

RUN_DATE TIME_IN_S DISTANCE_IN_KM 01 Apr 2020 310 1 02 Apr 2020 1,600 5 03 Apr 2020 3,580 11 06 Apr 2020 1,550 5 07 Apr 2020 300 1 10 Apr 2020 280 1 13 Apr 2020 1,530 5 14 Apr 2020 295 1 15 Apr 2020 292 1 this = prev + 1

Slide 10

Slide 10 text

RUN_DATE TIME_IN_S DISTANCE_IN_KM 01 Apr 2020 310 1 02 Apr 2020 1,600 5 03 Apr 2020 3,580 11 06 Apr 2020 1,550 5 07 Apr 2020 300 1 10 Apr 2020 280 1 13 Apr 2020 1,530 5 14 Apr 2020 295 1 15 Apr 2020 292 1 this = prev + 1 this = prev + 3

Slide 11

Slide 11 text

RUN_DATE TIME_IN_S DISTANCE_IN_KM 01 Apr 2020 310 1 02 Apr 2020 1,600 5 03 Apr 2020 3,580 11 06 Apr 2020 1,550 5 07 Apr 2020 300 1 10 Apr 2020 280 1 13 Apr 2020 1,530 5 14 Apr 2020 295 1 15 Apr 2020 292 1 this = prev + 1 this = prev + 3 this ≠ prev + 1

Slide 12

Slide 12 text

select * from running_log match_recognize ( ); input output

Slide 13

Slide 13 text

define consecutive as run_date = prev ( run_date ) + 1

Slide 14

Slide 14 text

pattern ( init consecutive* ) define consecutive as run_date = prev ( run_date ) + 1

Slide 15

Slide 15 text

pattern ( init consecutive* ) define consecutive as run_date = prev ( run_date ) + 1 Undefined => "Always true" > 0 matches

Slide 16

Slide 16 text

RUN_DATE VARIABLE TIME_IN_S DISTANCE_IN_KM 01 Apr 2020 INIT 310 1 02 Apr 2020 CONSECUTIVE 1,600 5 03 Apr 2020 CONSECUTIVE 3,580 11 06 Apr 2020 INIT 1,550 5 07 Apr 2020 CONSECUTIVE 300 1 10 Apr 2020 INIT 280 1 13 Apr 2020 INIT 1,530 5 14 Apr 2020 CONSECUTIVE 295 1 15 Apr 2020 CONSECUTIVE 292 1

Slide 17

Slide 17 text

pattern ( init consecutive* ) define consecutive as run_date = prev ( run_date ) + 1 Which row is prev?!

Slide 18

Slide 18 text

order by run_date pattern ( init consecutive* ) define consecutive as run_date = prev ( run_date ) + 1

Slide 19

Slide 19 text

match_recognize ( order by run_date measures first ( run_date ) as start_date, count (*) as days pattern ( init consecutive* ) define consecutive as run_date = prev ( run_date ) + 1 ); How many consecutive rows? First row in group

Slide 20

Slide 20 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | DEMO

Slide 21

Slide 21 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | Can you make that reusable?

Slide 22

Slide 22 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | ….

Slide 23

Slide 23 text

create view consecutive_days as select * from running_log match_recognize ( … ); Dynamic table names??

Slide 24

Slide 24 text

open cur for 'select * from ' || tab || ' match_recognize ( … ); SQL

Slide 25

Slide 25 text

open cur for 'select * from ' || dbms_assert.sql_object_name ( tab ) || ' match_recognize ( … );

Slide 26

Slide 26 text

blogs.oracle.com/sql www.youtube.com/c/TheMagicOfSQL @ChrisRSaxon SQL Macros

Slide 27

Slide 27 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | blogs.oracle.com/sql www.youtube.com/c/TheMagicOfSQL @ChrisRSaxon SQL Macros table scalar

Slide 28

Slide 28 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | blogs.oracle.com/sql www.youtube.com/c/TheMagicOfSQL @ChrisRSaxon SQL Macros table scalar in from clause "everywhere" else

Slide 29

Slide 29 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | blogs.oracle.com/sql www.youtube.com/c/TheMagicOfSQL @ChrisRSaxon SQL Macros table scalar in from clause "everywhere" else only these in 19.6

Slide 30

Slide 30 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | blogs.oracle.com/sql www.youtube.com/c/TheMagicOfSQL @ChrisRSaxon create function top_n ( num_rows number, tab dbms_tf.table_t ) return varchar2 sql_macro is begin select * from top_n.tab fetch first top_n.num_rows rows only

Slide 31

Slide 31 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | blogs.oracle.com/sql www.youtube.com/c/TheMagicOfSQL @ChrisRSaxon create function top_n ( num_rows number, tab dbms_tf.table_t ) return varchar2 sql_macro is begin return 'select * from top_n.tab fetch first top_n.num_rows rows only'; end;

Slide 32

Slide 32 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | blogs.oracle.com/sql www.youtube.com/c/TheMagicOfSQL @ChrisRSaxon create function top_n ( num_rows number, tab dbms_tf.table_t ) return varchar2 sql_macro is begin return 'select * from top_n.tab fetch first top_n.num_rows rows only'; end;

Slide 33

Slide 33 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | blogs.oracle.com/sql www.youtube.com/c/TheMagicOfSQL @ChrisRSaxon create function top_n ( num_rows number, tab dbms_tf.table_t ) return varchar2 sql_macro is begin return 'select * from top_n.tab fetch first top_n.num_rows rows only'; end; Find/replace

Slide 34

Slide 34 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | blogs.oracle.com/sql www.youtube.com/c/TheMagicOfSQL @ChrisRSaxon from top_n ( 5, employees ) from top_n ( :bind, jobs ) (19.6) from employees fetch first 5 rows only from jobs fetch first :bind rows only

Slide 35

Slide 35 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | blogs.oracle.com/sql www.youtube.com/c/TheMagicOfSQL @ChrisRSaxon from top_n ( 5, employees ) from top_n ( :bind, jobs ) (19.6) from employees fetch first 5 rows only from jobs fetch first :bind rows only

Slide 36

Slide 36 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | blogs.oracle.com/sql www.youtube.com/c/TheMagicOfSQL @ChrisRSaxon from top_n ( 5, employees ) from top_n ( :bind, jobs ) (19.6) from employees fetch first 5 rows only from jobs fetch first :bind rows only

Slide 37

Slide 37 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | blogs.oracle.com/sql www.youtube.com/c/TheMagicOfSQL @ChrisRSaxon create function get_consecutive_rows ( tab dbms_tf.table_t, col dbms_tf.columns_t ) return varchar2 sql_macro is begin return 'get_consecutive_rows.tab match_recognize ( … )'; end;

Slide 38

Slide 38 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | DEMO

Slide 39

Slide 39 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | We need to upgrade to 19c!

Slide 40

Slide 40 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | Can we have a meeting about that?

Slide 41

Slide 41 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | Person 09:00 10:00 11:00 12:00 13:00 14:00 15:00 16:00 1 2 Find the gap

Slide 42

Slide 42 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | Person 09:00 10:00 11:00 12:00 13:00 14:00 15:00 16:00 1 2 Find the gap [ start, end ) >= 09:00 < 12:00

Slide 43

Slide 43 text

How I know where the gaps are?

Slide 44

Slide 44 text

max end so far < next start

Slide 45

Slide 45 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | Person Start Time End Time Running Max 1 09:00 10:00 2 09:00 12:00 1 10:00 10:30 1 11:00 13:00 2 12:30 13:00 2 13:00 14:00 2 15:00 17:00 1 16:00 17:00

Slide 46

Slide 46 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | Person Start Time End Time Running Max 1 09:00 10:00 10:00 2 09:00 12:00 1 10:00 10:30 1 11:00 13:00 2 12:30 13:00 2 13:00 14:00 2 15:00 17:00 1 16:00 17:00

Slide 47

Slide 47 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | Person Start Time End Time Running Max 1 09:00 10:00 10:00 2 09:00 12:00 12:00 1 10:00 10:30 1 11:00 13:00 2 12:30 13:00 2 13:00 14:00 2 15:00 17:00 1 16:00 17:00

Slide 48

Slide 48 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | Person Start Time End Time Running Max 1 09:00 10:00 10:00 2 09:00 12:00 12:00 1 10:00 10:30 12:00 1 11:00 13:00 2 12:30 13:00 2 13:00 14:00 2 15:00 17:00 1 16:00 17:00

Slide 49

Slide 49 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | Person Start Time End Time Running Max 1 09:00 10:00 10:00 2 09:00 12:00 12:00 1 10:00 10:30 12:00 1 11:00 13:00 13:00 2 12:30 13:00 13:00 2 13:00 14:00 14:00 2 15:00 17:00 1 16:00 17:00

Slide 50

Slide 50 text

running max end < next start

Slide 51

Slide 51 text

match_recognize ( order by start_date, end_date measures max ( end_date ) as start_date, next ( start_date ) as end_date all rows per match pattern ( ( gap | {-no_gap-} )+ ) define gap as max ( end_date ) < next ( start_date ) );

Slide 52

Slide 52 text

match_recognize ( order by start_date, end_date measures max ( end_date ) as start_date, next ( start_date ) as end_date all rows per match pattern ( ( gap | {-no_gap-} )+ ) define gap as max ( end_date ) < next ( start_date ) ); {- exclude from output -}

Slide 53

Slide 53 text

match_recognize ( order by start_date, end_date measures max ( end_date ) as start_date, next ( start_date ) as end_date all rows per match pattern ( ( gap | {-no_gap-} )+ ) define gap as max ( end_date ) < next ( start_date ) );

Slide 54

Slide 54 text

match_recognize ( order by start_date, end_date measures max ( end_date ) as start_date, next ( start_date ) as end_date all rows per match pattern ( ( gap | {-no_gap-} )+ ) define gap as max ( end_date ) < next ( start_date ) );

Slide 55

Slide 55 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | blogs.oracle.com/sql www.youtube.com/c/TheMagicOfSQL @ChrisRSaxon create function find_gaps ( tab dbms_tf.table_t, date_cols dbms_tf.columns_t ) return varchar2 sql_macro is begin return 'find_gaps.tab match_recognize ( … )'; end;

Slide 56

Slide 56 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | DEMO

Slide 57

Slide 57 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | SQL for date ranges, gaps and overlaps https://stewashton.wordpress.com/2014/03/11/sql- for-date-ranges-gaps-and-overlaps/ Oracle 20c: SQL Macros https://blog.sqlora.com/en/oracle-20c-sql-macros/ Further Reading

Slide 58

Slide 58 text

Copyright © 2020, Oracle and/or its affiliates. All rights reserved. | asktom.oracle.com #MakeDataGreatAgain Ryan McGuire / Gratisography