powerful oracle 12c sql features - themis training
TRANSCRIPT
Powerful Oracle 12c
SQL Features
John Mullins
www.themisinc.com
Presenter
John Mullins
Themis Inc. ([email protected])
30+ years of Oracle experience
Oracle Certified Professional DBA
Certified Technical Trainer
Over 250 classes taught
Themis Inc.
More than 18 years in the industry
Committed to your training
Courses:
DB2, SQL Server, Oracle, Unix, Linux, Java, Web
Development, .NET and many more
www.themisinc.com
Related / Upcoming Themis
Courses
Oracle 12c New Features for Developers
Online class May 6-7
Oracle 12c New Features for Administrators
Online class May 11-13
Introduction to Oracle SQL
Oracle Advanced SQL
Webinar Objectives
Gain an understanding of some of the SQL
new features in Oracle 12c
Row Pattern Matching
Search for pattern matches within a column but
across more than one row
Usage
Trend Analysis
Identify Data Patterns (esp. in large data sets)
Quality Control Processing
Market Analysis
Row Pattern Matching
Clauses and Options
MATCH_RECOGNIZE
PARTITION BY
ORDER BY
MEASURES
ROW PATTERN ROWS PER MATCH
PATTERN
DEFINE
Row Pattern Matching Examples
Table: DAILY_ATTENDANCEATT_DATE ATTENDANCE
--------- ----------
01-JAN-14 1714
02-JAN-14 1238
03-JAN-14 1004
04-JAN-14 4656
…
24-DEC-14 1349
25-DEC-14 0
26-DEC-14 1147
27-DEC-14 6577
…
Row Pattern Matching Example 1
Find patterns of declining attendance of 3
straight days
Declining Attendance Example
SELECT * FROM daily_attendance
MATCH_RECOGNIZE (
MEASURES
A.attendance_date AS high_date,
LAST (DOWN.attendance) low_attendance,
LAST (DOWN.attendance_date) low_date
ONE ROW PER MATCH
AFTER MATCH SKIP TO NEXT ROW
PATTERN (A DOWN {3})
DEFINE
DOWN AS DOWN.attendance < PREV (DOWN.attendance))
arep
ORDER BY low_date;
MEASURES and PATTERN
Clauses
MEASURES
Navigation Functions
PREV, NEXT, FIRST, LAST
PATTERN
Iteration Quantifiers
* zero or more, + one or more, ? zero or one
{n,} n or more, {n,m} between n and m
{,m} between zero and m
{n} exactly n
Declining Attendance Example
HIGH_DATE LOW_ATTENDANCE LOW_DATE
--------- -------------- ---------
11-JAN-14 1001 14-JAN-14
01-FEB-14 1095 04-FEB-14
09-FEB-14 1043 12-FEB-14
15-MAR-14 1680 18-MAR-14
22-MAR-14 1209 25-MAR-14
23-MAR-14 1065 26-MAR-14
12-APR-14 1401 15-APR-14
19-APR-14 1116 22-APR-14
07-JUN-14 1141 10-JUN-14
08-JUN-14 1018 11-JUN-14
Row Pattern Matching Example 2
Find patterns where there is a drop in
attendance from one day to the next of more
than 75%
Do not include the closed dates
Attendance Drop % Example
SELECT * FROM
(select * from daily_attendance where attendance <> 0)
MATCH_RECOGNIZE (
MEASURES
A.attendance_date AS adt,
A.attendance AS aatt,
B.attendance AS batt,
((b.attendance - a.attendance) * 100) / a.attendance AS
pctdrop
ONE ROW PER MATCH
AFTER MATCH SKIP TO B
PATTERN (A B)
DEFINE
B AS (b.attendance - a.attendance) / a.attendance < -0.75);
Attendance Drop % Example
ADT AATT BATT PCTDROP
--------- ---------- ---------- ----------
05-JAN-14 4707 1118 -76.248141
09-MAR-14 6559 1021 -84.433603
30-MAR-14 6374 1065 -83.291497
04-MAY-14 6450 1354 -79.007752
11-MAY-14 5557 1012 -81.788735
18-MAY-14 6484 1027 -84.161012
25-MAY-14 6715 1274 -81.02755
01-JUN-14 6490 1371 -78.875193
Row Pattern Matching Example 3
Calculate a running total of attendance
The running total cannot exceed 100,000
ie: Create blocks of attendance under 100,000
Running Total Example
SELECT * FROM daily_attendance
MATCH_RECOGNIZE (
ORDER BY attendance_date
MEASURES
FIRST (attendance_date) AS fdate,
LAST (attendance_date) AS ldate,
SUM (attendance) AS sum_att
ONE ROW PER MATCH
AFTER MATCH SKIP PAST LAST ROW
PATTERN (A+)
DEFINE
A AS SUM(attendance) <= 100000 );
Running Total Example
FDATE LDATE SUM_ATT
--------- --------- ----------
01-JAN-14 09-FEB-14 98506
10-FEB-14 21-MAR-14 98223
22-MAR-14 26-APR-14 95917
27-APR-14 31-MAY-14 96589
01-JUN-14 11-JUL-14 99725
12-JUL-14 17-AUG-14 98880
18-AUG-14 26-SEP-14 99817
27-SEP-14 31-OCT-14 97589
01-NOV-14 05-DEC-14 93841
06-DEC-14 31-DEC-14 65221
Row Pattern Matching Example 4
Credit Card Transactions
Find patterns where the same credit card
number was used physically in two different
locations (states) on the same day
Credit Card Location Example
SELECT * FROM cc_trans
MATCH_RECOGNIZE (
PARTITION BY cc_num
ORDER BY trans_date, loc_id
MEASURES
A.cc_num AS ccnum,
a.trans_date AS tdate
ONE ROW PER MATCH
AFTER MATCH SKIP TO NEXT ROW
PATTERN (A B+)
DEFINE
B as TRUNC(trans_date) = PREV (TRUNC(trans_date)) AND
loc_id <> PREV (loc_ID)
) trans
ORDER BY tdate;
Credit Card Location Example
CCNUM Multiple Trans on:
----------------- --------------------
2100455578809090 03-MAR-15
1000340022208990 04-MAR-15
Top-n Queries
Row Limiting Clause
FETCH FIRST/NEXT and OFFSET Clauses
Retrieve first n records from a result set
Retrieve the first n records after skipping over a set
of records
ANSI Standard
FETCH FIRST Clause
Example:
Return the 5 attendance dates with the highest
attendances
FETCH FIRST Clause
SELECT * FROM daily_attendance
ORDER BY attendance desc
FETCH FIRST 5 ROWS ONLY;
ATT_DATE ATTENDANCE
--------- ----------
19-OCT-14 6972
27-SEP-14 6941
01-NOV-14 6941
20-JUL-14 6892
08-NOV-14 6891
FETCH FIRST Clause
SELECT * FROM daily_attendance
ORDER BY attendance desc
FETCH FIRST 5 ROWS WITH TIES;
ATT_DATE ATTENDANCE
--------- ----------
19-OCT-14 6972
27-SEP-14 6941
01-NOV-14 6941
20-JUL-14 6892
08-NOV-14 6891
01-DEC-14 6891
FETCH FIRST Clause
SELECT * FROM daily_attendance
ORDER BY attendance desc
FETCH FIRST 3 PERCENT ROWS ONLY;
ATT_DATE ATTENDANCE
--------- ----------
19-OCT-14 6972
01-NOV-14 6941
27-SEP-14 6941
20-JUL-14 6892
01-DEC-14 6891
08-NOV-14 6891
19-APR-14 6844
05-OCT-14 6829
23-FEB-14 6780
22-NOV-14 6725
25-MAY-14 6715
OFFSET Clause
SELECT * FROM daily_attendance
ORDER BY attendance desc
OFFSET 5 ROWS
FETCH NEXT 3 ROWS ONLY;
ATT_DATE ATTENDANCE
--------- ----------
01-DEC-14 6891
19-APR-14 6844
05-OCT-14 6829
UNION / UNION ALL
Concurrent Execution of Branches
Turned on automatically and entire UNION or
UNION ALL is processed in parallel if:
OPTIMIZER_FEATURE_ENABLED set to
12.1 or higher
New Hints (For non-12c Optimizer)
PQ_CONNCURRENT_UNION
NO_PQ_CONNCURRENT_UNION
Other Oracle 12c SQL New
Features
Cascading TRUNCATETRUNCATE TABLE tname CASCADE;
Identity ColumnsCREATE TABLE acct_trans
(trans_id NUMBER GENERATED AS identity PRIMARY KEY,
trans_date DATE);
Invisible ColumnsCREATE TABLE secure_data
(secure_id NUMBER,
ssn NUMBER INVISIBLE);
Summary
Oracle 12c introduces several powerful and
useful SQL new features
Questions?
Themis Oracle Classes
Classes are offered in a public classroom, onsite at your
location, or live on the internet Oracle 12c New Features for Developers
Online class May 6-7
Oracle 12c New Features for Administrators
Online class May 11-13
Oracle SQL, Oracle Advanced SQL
Oracle PL/SQL, Oracle Advanced PL/SQL
Oracle SQL Optimization
Oracle Database Performance Tuning for Administrators
And many more…
For More Information
Visit the Themis web site www.themisinc.com
John Caccavale
908-233-8900
To get a copy of the presentation:
http://www.themisinc.com/webinars
Thank you for attending. Have a good day.