Invoking analytic functions
You invoke analytic functions as follows:.
You invoke analytic functions as follows:
analytic-function(arguments) OVER(
[ window-partition-clause ]
[ window-order-clause [ window-frame-clause ] ]
)
An analytic function's OVER
clause contains up to three sub-clauses, which specify how to partition and sort function input, and how to frame input with respect to the current row. Function input is the result set that the query returns after it evaluates FROM
, WHERE
, GROUP BY
, and HAVING
clauses.
Note
Each function has its ownOVER
clause requirements. For example, some analytic functions do not support window order and window frame clauses.
For syntax details, see Analytic functions.
Function execution
An analytic function executes as follows:
- Takes the input rows that the query returns after it performs all joins, and evaluates
FROM
,WHERE
,GROUP BY
, andHAVING
clauses. - Groups input rows according to the window partition (
PARTITION BY
) clause. If this clause is omitted, all input rows are treated as a single partition. - Sorts rows within each partition according to window order (
ORDER BY
) clause. - If the
OVER
clause includes a window order clause, the function checks for a window frame clause and executes it as it processes each input row. If theOVER
clause omits a window frame clause, the function treats the entire partition as a window frame.
Restrictions
-
Analytic functions are allowed only in a query's
SELECT
andORDER BY
clauses. -
Analytic functions cannot be nested. For example, the following query throws an error:
=> SELECT MEDIAN(RANK() OVER(ORDER BY sal) OVER()).