0% found this document useful (0 votes)
73 views18 pages

Analitical Function

Analytic Functions : A Developer’s Best Friend - Analytic functions allow computations across groups of rows such as calculating aggregates without grouping. They were introduced in Oracle 8i. - Common analytic functions include RANK, DENSE_RANK, ROW_NUMBER, LAG, LEAD, FIRST_VALUE and LAST_VALUE. - Syntax includes query partitions, order clauses, and windowing to define groups of rows and ordering.

Uploaded by

Antoni Sagayaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
73 views18 pages

Analitical Function

Analytic Functions : A Developer’s Best Friend - Analytic functions allow computations across groups of rows such as calculating aggregates without grouping. They were introduced in Oracle 8i. - Common analytic functions include RANK, DENSE_RANK, ROW_NUMBER, LAG, LEAD, FIRST_VALUE and LAST_VALUE. - Syntax includes query partitions, order clauses, and windowing to define groups of rows and ordering.

Uploaded by

Antoni Sagayaraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Analytic Functions :

A Developer’s Best Friend


Tim Hall
Oracle ACE Director
Oracle ACE of the Year 2006
OakTable Network
OCP DBA (7, 8, 8i, 9i, 10g, 11g, 12c)
OCP Advanced PL/SQL Developer
Oracle Database: SQL Certified Expert

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.

§ They compute aggregate values based on groups


of data.

§ Unlike aggregate functions, they don’t reduce the


number of rows returned.

§ Analytic functions are processed after the result


set is returned, but before the conventional
ORDER BY operation.

average.sql

https://oracle-base.com
Analytic Function Syntax
§ There is some variation between individual functions, but the basic syntax is similar.

§ The analytic clause is broken down as follows.

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.

RANGE BETWEEN start_point AND end_point


ROWS BETWEEN start_point AND end_point

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.

RANGE BETWEEN start_point AND end_point


ROWS BETWEEN start_point AND end_point

§ Start and end points.


§ UNBOUNDED PRECEDING : Window starts at first row of partition.
§ UNBOUNDED FOLLOWING : Window ends at last row of partition
§ CURRENT ROW : Window starts or ends at the current row
§ value_expr PRECEDING : Physical/logical offset before current row.
§ value_expr FOLLOWING : Physical/logical offset after current row.

§ Default is “RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW”


windowing_clause.sql

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.

§ Makes Top-N Queries and resultset paging easy.

SELECT empno, sal


FROM scott.emp
ORDER BY sal DESC
FETCH FIRST 5 ROWS ONLY;

How is this implemented?

https://oracle-base.com
Top-N Queries (12c)
CONN / AS SYSDBA

ALTER SESSION SET EVENTS '10053 trace name context forever, level 1';

SELECT empno, sal


FROM scott.emp
ORDER BY sal DESC
FETCH 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"<=5
ORDER BY "from$_subquery$_002"."rowlimit_$_ 0" DESC

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.

§ Takes time to get used to the analytic clause, so keep playing!

https://oracle-base.com
The End…
§ Slides and Demos:

http://www.oracle-base.com/workshops

§ Questions?

https://oracle-base.com

You might also like