Table of Contents
Oracle Database - SQL - Analytic Functions
About
Analytic functions in the Oracle Database Context
Articles Related
The process
Query processing using analytic functions takes place in three stages :
- all joins, WHERE, GROUP BY and HAVING clauses are performed.
- the result set is made available to the analytic functions, and all their calculations take place.
- if the query has an ORDER BY clause at its end, the ORDER BY is processed to allow for precise output ordering.
Syntax
FUNCTION() over (partition BY mycolumns ORDER BY mycolumns)
An analytic function takes place after that the original data set is retrieved.
The clause <partition by> defines how the function is calculated (aggregated for an aggregate function). For a sum for instance, the following statement:
SUM(MyColumn) over (partition BY Year)
will do a sum by year (partition = group by).
If the PARTITION BY clause is absent, then the function is computed over the whole query result set.
Functions
List of analytic functions
- AVG
- CORR
- COVAR_POP
- COVAR_SAMP
- COUNT
- CUME_DIST
- DENSE_RANK
- FIRST
- LAST
- LAST_VALUE
- MAX/MIN
- NTILE
- PERCENT_RANK
- PERCENTILE_CONT
- PERCENTILE_DISC
- RATIO_TO_REPORT
- REGR_ (Linear Regression) Functions
- STDDEV
- STDDEV_POP
- STDDEV_SAMP
- SUM : Analytic Function Sum
- VAR_POP
- VAR_SAMP
- VARIANCE
Analytics and Aggregate function
On the HR sample schema.
SELECT j.job_title, COUNT(*), MAX(salary) maxsalary, MIN(salary) minsalary, RANK() OVER(ORDER BY MAX(salary)) rankorder FROM employees e, jobs j WHERE e.job_id = j.job_id GROUP BY j.job_title ORDER BY rankorder;
