http://www.oracle-base.com
Analytic Functions :Analytic Functions :An Oracle Developer’s Best FriendAn Oracle Developer’s Best Friend
Tim HallTim Hall
Oracle ACE DirectorOracle ACE Director
Oracle ACE of the Year 2006Oracle ACE of the Year 2006
OakTable NetworkOakTable NetworkOCP DBA (7, 8, 8i, 9i, 10g, 11g)OCP DBA (7, 8, 8i, 9i, 10g, 11g)
OCP Advanced PL/SQL DeveloperOCP Advanced PL/SQL Developer
Oracle Database: SQL Certified ExpertOracle Database: SQL Certified Expert
http://www.oracle-base.com
BooksBooksOracle PL/SQL TuningOracle PL/SQL TuningOracle Job SchedulingOracle Job Scheduling
http://www.oracle-base.com
What are Analytic Functions?What are Analytic Functions?
• Analytic Functions, or Windowing Functions, were introduced in Analytic Functions, or Windowing Functions, were introduced in Oracle8i.Oracle8i.
• They compute aggregate values based on groups of data.They compute aggregate values based on groups of data.
• Unlike aggregate functions, they don’t reduce the number of rows Unlike aggregate functions, they don’t reduce the number of rows returned.returned.
• Analytic functions are processed after the result set is returned, but Analytic functions are processed after the result set is returned, but before the conventional ORDER BY operation.before the conventional ORDER BY operation. ((average.sql))
http://www.oracle-base.com
Analytic Function SyntaxAnalytic Function Syntax
• There is some variation between individual functions, but the basic There is some variation between individual functions, but the basic syntax is similar.syntax is similar.
• The analytic clause is broken down as follows.The analytic clause is broken down as follows.
http://www.oracle-base.com
Analytic Function SyntaxAnalytic Function Syntax
http://www.oracle-base.com
query_partition_clausequery_partition_clause
• Divides data into partitions or groups, similar to GROUP BY.Divides data into partitions or groups, similar to GROUP BY.
• The operation of the analytic function is restricted to the boundary The operation of the analytic function is restricted to the boundary imposed by these partition.imposed by these partition.
http://www.oracle-base.com
query_partition_clausequery_partition_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
http://www.oracle-base.com
query_partition_clausequery_partition_clause
• Divides data into partitions or groups, similar to GROUP BY.Divides data into partitions or groups, similar to GROUP BY.
• The operation of the analytic function is restricted to the boundary The operation of the analytic function is restricted to the boundary imposed by these partition.imposed by these partition.
• If the query_partition_clause is empty, the partition is assumed to be If the query_partition_clause is empty, the partition is assumed to be the whole result set.the whole result set.
((query_partition_clause.sql))
http://www.oracle-base.com
order_by_clauseorder_by_clause
• Orders rows, or siblings, within a partition.Orders rows, or siblings, within a partition.
http://www.oracle-base.com
order_by_clauseorder_by_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
1 2
http://www.oracle-base.com
order_by_clauseorder_by_clause
• Orders rows, or siblings, within a partition.Orders rows, or siblings, within a partition.
• Necessary with order-sensitive analytic functions.Necessary with order-sensitive analytic functions.
((order_by_clause.sql))
• Ordering NULLs:Ordering NULLs:– ASC is default.ASC is default.– When ASC is used, NULLS LAST is default.When ASC is used, NULLS LAST is default.– When DESC is used, NULLS FIRST is default.When DESC is used, NULLS FIRST is default.
• order_by_clause affects processing orderorder_by_clause affects processing orderbut may not affect display order consistently.but may not affect display order consistently.MUST use an ORDER BY!MUST use an ORDER BY!
http://www.oracle-base.com
windowing_clausewindowing_clause
• Extension of the order_by_clause, giving finer control of the window of Extension of the order_by_clause, giving finer control of the window of operation for some functions.operation for some functions.
RANGE BETWEEN start_point AND end_pointRANGE BETWEEN start_point AND end_pointROWS BETWEEN start_point AND end_pointROWS BETWEEN start_point AND end_point
http://www.oracle-base.com
windowing_clausewindowing_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
http://www.oracle-base.com
windowing_clausewindowing_clause
• Extension of the order_by_clause, giving finer control of the window of Extension of the order_by_clause, giving finer control of the window of operation for some functions.operation for some functions.
RANGE BETWEEN start_point AND end_pointRANGE BETWEEN start_point AND end_pointROWS BETWEEN start_point AND end_pointROWS BETWEEN start_point AND end_point
• Start and end points.Start and end points.
– UNBOUNDED PRECEDING : Window starts at first row of partition. UNBOUNDED PRECEDING : Window starts at first row of partition.
– UNBOUNDED FOLLOWING : Window ends at last row of partitionUNBOUNDED FOLLOWING : Window ends at last row of partition
– CURRENT ROW : Window starts or ends at the current rowCURRENT ROW : Window starts or ends at the current row
– value_expr PRECEDING : Physical/logical offset before current row.value_expr PRECEDING : Physical/logical offset before current row.
– value_expr FOLLOWING : Physical/logical offset before current row.value_expr FOLLOWING : Physical/logical offset before current row.
• Default is “RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT Default is “RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW”ROW”((windowing_clause.sql))
http://www.oracle-base.com
Analytic FunctionsAnalytic Functions
• AVG *AVG *• CORR *CORR *• COUNT *COUNT *• COVAR_POP *COVAR_POP *• COVAR_SAMP *COVAR_SAMP *• CUME_DISTCUME_DIST• DENSE_RANKDENSE_RANK• FIRSTFIRST• FIRST_VALUE *FIRST_VALUE *• LAGLAG• LASTLAST• LAST_VALUE *LAST_VALUE *• LEADLEAD• LISTAGGLISTAGG• MAX *MAX *• MIN *MIN *• NTH_VALUE *NTH_VALUE *
• NTILENTILE• PERCENT_RANKPERCENT_RANK• PERCENTILE_CONTPERCENTILE_CONT• PERCENTILE_DISCPERCENTILE_DISC• RANKRANK• RATIO_TO_REPORTRATIO_TO_REPORT• REGR_ (Linear Regression) REGR_ (Linear Regression)
Functions *Functions *• ROW_NUMBERROW_NUMBER• STDDEV *STDDEV *• STDDEV_POP *STDDEV_POP *• STDDEV_SAMP *STDDEV_SAMP *• SUM *SUM *• VAR_POP *VAR_POP *• VAR_SAMP *VAR_SAMP *• VARIANCE *VARIANCE *
* = full syntax
http://www.oracle-base.com
Analytic Functions : ExamplesAnalytic Functions : Examples
• ranking.sql
• first_last.sqlfirst_last.sql
• listagg.sqllistagg.sql
• lag_lead.sqllag_lead.sql
• first_value.sqlfirst_value.sql
• last_value.sqllast_value.sql
• row_number.sqlrow_number.sql
http://www.oracle-base.com
Top-N Queries (12c)Top-N Queries (12c)
SELECT empno, salFROM scott.empORDER BY sal DESCFETCH FIRST 5 ROWS ONLY;
• Oracle Database 12c finally has a row limiting clause.Oracle Database 12c finally has a row limiting clause.
• Makes Top-N Queries and resultset paging easy.Makes Top-N Queries and resultset paging easy.
• How is this implemented?How is this implemented?
http://www.oracle-base.com
Top-N Queries (12c)Top-N Queries (12c)
CONN / AS SYSDBA
ALTER SESSION SET EVENTS '10053 trace name context forever, level 1';
SELECT empno, salFROM scott.empORDER BY sal DESCFETCH FIRST 5 ROWS ONLY;
ALTER SESSION SET EVENTS '10053 trace name context off’;
Final query after transformations:******* UNPARSED QUERY IS *******
SELECT "from$_subquery$_002"."EMPNO" "EMPNO", "from$_subquery$_002"."SAL" "SAL"FROM (SELECT "EMP"."EMPNO" "EMPNO", "EMP"."SAL" "SAL", "EMP"."SAL" "rowlimit_$_ 0", ROW_NUMBER() OVER ( ORDER BY "EMP"."SAL" DESC ) "rowlimit_$$_rownumber" FROM "SCOTT"."EMP" "EMP") "from$_subquery$_002"WHERE "from$_subquery$_002"."rowlimit_$$_rownumber"<=5ORDER BY "from$_subquery$_002"."rowlimit_$_ 0" DESC
http://www.oracle-base.com
SummarySummary
• Analytic Functions make complex post-query processing simple.Analytic Functions make complex post-query processing simple.
• Typically much faster than performing similar operations using Typically much faster than performing similar operations using procedural languages.procedural languages.
• Very flexible.Very flexible.
• Takes time to get used to the analytic clause, so keep playing!Takes time to get used to the analytic clause, so keep playing!
http://www.oracle-base.com
The End… The End…
• Slides and Demos:Slides and Demos:
http://www.oracle-base.com/workshopshttp://www.oracle-base.com/workshops
• Questions?Questions?