Analitical Function
Analitical Function
https://oracle-base.com
Books
Oracle PL/SQL Tuning
Oracle Job Scheduling
https://oracle-base.com
https://oracle-base.com
https://oracle-base.com
What are Analytic Functions?
§ Analytic Functions, or Windowing Functions, were
introduced in Oracle 8i.
average.sql
https://oracle-base.com
Analytic Function Syntax
§ There is some variation between individual functions, but the basic syntax is similar.
https://oracle-base.com
Analytic Function Syntax
https://oracle-base.com
query_partition_clause
§ Divides data into partitions or groups, similar
to GROUP BY. EMPNO DEPTNO SAL
---------- ---------- ----------
7934 10 1300
§ The operation of the analytic function is 7782 10 2450
restricted to the boundary imposed by these 7839 10 5000
partition. 7369 20 800
7876 20 1100
7566 20 2975
§ If the query_partition_clause is empty, the 7788 20 3000
partition is assumed to be the whole result 7902 20 3000
7900 30 950
set. 7654 30 1250
7521 30 1250
query_partition_clause.sql 7844 30 1500
7499 30 1600
7698 30 2850
https://oracle-base.com
order_by_clause
§ Orders rows, or siblings, within a partition.
EMPNO DEPTNO SAL
---------- ---------- ---------- 1 2
§ Necessary with order-sensitive analytic 7934 10 1300
functions. 7782 10 2450
7839 10 5000
7369 20 800
§ Ordering NULLs: 7876 20 1100
§ ASC is default. 7566 20 2975
§ When ASC is used, NULLS LAST is default. 7788 20 3000
§ When DESC is used, NULLS FIRST is default. 7902 20 3000
7900 30 950
7654 30 1250
§ order_by_clause affects processing order 7521 30 1250
but may not affect display order 7844 30 1500
consistently. MUST use an ORDER BY! 7499 30 1600
order_by_clause.sql 7698 30 2850
https://oracle-base.com
windowing_clause
§ Extension of the order_by_clause, giving finer control of the window of operation for
some functions.
https://oracle-base.com
windowing_clause
EMPNO DEPTNO SAL
---------- ---------- ----------
7934 10 1300
7782 10 2450
7839 10 5000
7369 20 800
7876 20 1100
7566 20 2975
7788 20 3000
7902 20 3000
7900 30 950
7654 30 1250
7521 30 1250
7844 30 1500
7499 30 1600
7698 30 2850
https://oracle-base.com
windowing_clause
§ Extension of the order_by_clause, giving finer control of the window of operation for
some functions.
https://oracle-base.com
Analytic Functions
§ AVG * § LEAD
§ CLUSTER_DETAILS § LISTAGG
§ CLUSTER_DISTANCE § MAX *
§ MIN *
§ CLUSTER_ID § NTH_VALUE *
§ CLUSTER_PROBABILITY
* = full syntax
§ NTILE
§ CLUSTER_SET § PERCENT_RANK
§ CORR * § PERCENTILE_CONT
§ COUNT * § PERCENTILE_DISC
§ PREDICTION
§ COVAR_POP * § PREDICTION_COST
§ COVAR_SAMP * § PREDICTION_DETAILS
§ CUME_DIST § PREDICTION_PROBABILITY
§ DENSE_RANK § PREDICTION_SET
§ FEATURE_DETAILS § RANK
§ RATIO_TO_REPORT
§ FEATURE_ID § REGR_ (Linear Regression) Functions *
§ FEATURE_SET § ROW_NUMBER
§ FEATURE_VALUE § STDDEV *
§ FIRST § STDDEV_POP *
§ FIRST_VALUE * § STDDEV_SAMP *
§ SUM *
§ LAG § VAR_POP *
§ LAST § VAR_SAMP *
§ LAST_VALUE * § VARIANCE *
https://oracle-base.com
Analytic Functions : Examples
§ ranking.sql
§ row_number.sql
§ listagg.sql
§ lag_lead.sql
§ first_value.sql
§ last_value.sql
§ corr.sql
https://oracle-base.com
Top-N Queries (12c)
§ Oracle Database 12c finally has a row limiting clause.
https://oracle-base.com
Top-N Queries (12c)
CONN / AS SYSDBA
ALTER SESSION SET EVENTS '10053 trace name context forever, level 1';
https://oracle-base.com
MATCH_RECOGNIZE (12c)
SELECT *
FROM sales_history MATCH_RECOGNIZE (
PARTITION BY product
ORDER BY tstamp
MEASURES STRT.tstamp AS start_tstamp,
LAST(UP.tstamp) AS peak_tstamp,
LAST(DOWN.tstamp) AS end_tstamp,
MATCH_NUMBER() AS mno
ONE ROW PER MATCH
AFTER MATCH SKIP TO LAST DOWN
PATTERN (STRT UP+ FLAT* DOWN+)
DEFINE
UP AS UP.units_sold > PREV(UP.units_sold),
FLAT AS FLAT.units_sold = PREV(FLAT.units_sold),
DOWN AS DOWN.units_sold < PREV(DOWN.units_sold)
) MRORDER BY MR.product, MR.start_tstamp;
https://oracle-base.com
Summary
§ Analytic Functions make complex post-query processing simple.
§ Typically much faster than performing similar operations using procedural languages.
§ Very flexible.
https://oracle-base.com
The End…
§ Slides and Demos:
http://www.oracle-base.com/workshops
§ Questions?
https://oracle-base.com