Analytic Functions
Analytic functions compute an aggregate value based on a group of rows. They differ from aggregate functions in that they return multiple rows for each group. The group of rows is called a window and is defined by the analytic_clause
.
Analytic functions are the last set of operations performed in a query except for the final ORDER BY
clause. All joins, WHERE
, GROUP BY
, and HAVING
clauses are completed before the analytic functions are processed. The final ORDER BY
clause is used to order the result of analytic functions. Analytic functions can appear in the select list of a query or subquery and in the ORDER
BY
clause.
Analytic functions allow you to divide query result sets into groups of rows called partitions. You can define partitions on columns or expressions. You can partition a query result set into just one partition holding all rows, a few large partitions or many small partitions holding just a few rows each.
You can define a sliding window for each row in the partition. This window determines the range of rows used to perform the calculations for the current row. Window sizes are based on a physical number of rows. The window has a starting row and an ending row and the window may move at one or both ends. For example, a window defined for a cumulative sum function would have its starting row fixed at the first row of the partition and the ending rows would slide from the start point to the last row of the partition. In contrast, a window defined for a moving average would have both the start point and end point slide.
You can set the window as large as all the rows in the partition or as small as one row within a partition.
You can specify multiple ordering expressions within each function. This is useful when using functions that rank values because the second expression can resolve ties between identical values for the first expression.
Analytic functions are commonly used to compute cumulative, moving, centered, and reporting aggregates.
Restrictions:
-
Analytic functions are not supported in materialized views.
The list of analytic functions follows. Functions followed by an asterisk (*) support the WindowingClause
.
SQL Syntax
Analytic function syntax:
AnalyticFunctionName ([arguments
]) OVER ([AnalyticClause]) AnalyticClause::= QueryPartitionClause [ORDER BY OrderByClause [,...] [WindowingClause]] | ORDER BY OrderByClause [,...] [WindowingClause] QueryPartitionClause::= PARTITION BY {Expression
[,Expression
]... | (Expression
[,Expression
]...) } OrderByClause::=Expression
[ASC|DESC] [NULLS {FIRST|LAST}] WindowingClause::= ROWS { BETWEEN StartPoint AND EndPoint | StartPoint } StartPoint::= UNBOUNDED PRECEDING | CURRENT ROW |PosNumConstantExpr
{ PRECEDING | FOLLOWING } EndPoint::= UNBOUNDED FOLLOWING | CURRENT ROW |PosNumConstantExpr
{ PRECEDING | FOLLOWING }
Parameters
Parameter | Description |
---|---|
|
Name of analytic function. |
|
Arguments for the analytic function. Number of arguments depends on the analytic function. Refer to the particular function for specific information on the arguments to the function. |
|
Indicates that the function is an analytic function. This clause is computed after the If you do not specify the |
|
Optional clause used in You can specify multiple analytic functions in the same query using either the same or different Valid values for |
|
Optional clause used in You can order the values in a partition on multiple keys each defined by Analytic functions operate in the order specified in this clause. However this clause does not guarantee the order of the result. Use the If you specify the If you do not specify the |
|
Specifies the ordering sequence (ascending or descending). Clause is optional. |
|
Specifies whether rows that contain Clause is optional. |
|
Clause is denoted by the You cannot specify the The value returned by the analytic function may produce nondeterministic results unless the ordering sequence results in unique ordering. In this case, specify multiple columns in the For the list of functions that allow the |
|
Use the If you omit the |
|
Valid values are
|
|
Valid values are
|
|
Use Cannot be used as the end point. |
|
Use Cannot be used as the start point. |
|
As a start point, As an end point, |
|
If The end point
|