Analytic functions
Note
All analytic functions in this section with an aggregate counterpart are appended with [Analytics] in the heading to avoid confusion between the two function types.Vertica analytics are SQL functions based on the ANSI 99 standard. These functions handle complex analysis and reporting tasks—for example:
-
Rank the longest-standing customers in a particular state.
-
Calculate the moving average of retail volume over a specified time.
-
Find the highest score among all students in the same grade.
-
Compare the current sales bonus that salespersons received against their previous bonus.
Analytic functions return aggregate results but they do not group the result set. They return the group value multiple times, once per record. You can sort group values, or partitions, using a window ORDER BY clause, but the order affects only the function result set, not the entire query result set.
Syntax
General
analytic-function(arguments) OVER(
[ window-partition-clause ]
[ window-order-clause [ window-frame-clause ] ]
)
With named window
analytic-function(arguments) OVER(
[ named-window [ window-frame-clause ] ]
)
Parameters
analytic-function(arguments)- A Vertica analytic function and its arguments.
OVER- Specifies how to partition, sort, and window frame function input with respect to the current row. The input data is the result set that the query returns after it evaluates
FROM,WHERE,GROUP BY, andHAVINGclauses.An empty
OVERclause provides the best performance for single threaded queries on a single node. - window-partition-clause
- Groups input rows according to one or more columns or expressions.
If you omit this clause, no grouping occurs and the analytic function processes all input rows as a single partition.
- window-order-clause
- Optionally specifies how to sort rows that are supplied to the analytic function. If the
OVERclause also includes a partition clause, rows are sorted within each partition. -
window-frame-clause - Only valid for some analytic functions, specifies as input a set of rows relative to the row that is currently being evaluated by the analytic function. After the function processes that row and its window, Vertica advances the current row and adjusts the window boundaries accordingly.
named-window- The name of a window that you define in the same query with a window-name-clause. This definition encapsulates window partitioning and sorting. Named windows are useful when the query invokes multiple analytic functions with similar
OVERclauses.A window name clause cannot specify a window frame clause. However, you can qualify the named window in an
OVERclause with a window frame clause.
Requirements
The following requirements apply to analytic functions:
-
All require an
OVERclause. Each function has its ownOVERclause requirements. For example, you can supply an emptyOVERclause for some analytic aggregate functions such asSUM. For other functions, window frame and order clauses might be required, or might be invalid. -
Analytic functions can be invoked only in a query's
SELECTandORDER BYclauses. -
Analytic functions cannot be nested. For example, the following query is not allowed:
=> SELECT MEDIAN(RANK() OVER(ORDER BY sal) OVER()). -
WHERE,GROUP BYandHAVINGoperators are technically not part of the analytic function. However, they determine input to that function.
See also
In this section
- ARGMAX [analytic]
- ARGMIN [analytic]
- AVG [analytic]
- BOOL_AND [analytic]
- BOOL_OR [analytic]
- BOOL_XOR [analytic]
- CONDITIONAL_CHANGE_EVENT [analytic]
- CONDITIONAL_TRUE_EVENT [analytic]
- COUNT [analytic]
- CUME_DIST [analytic]
- DENSE_RANK [analytic]
- EXPONENTIAL_MOVING_AVERAGE [analytic]
- FIRST_VALUE [analytic]
- LAG [analytic]
- LAST_VALUE [analytic]
- LEAD [analytic]
- MAX [analytic]
- MEDIAN [analytic]
- MIN [analytic]
- NTH_VALUE [analytic]
- NTILE [analytic]
- PERCENT_RANK [analytic]
- PERCENTILE_CONT [analytic]
- PERCENTILE_DISC [analytic]
- RANK [analytic]
- ROW_NUMBER [analytic]
- STDDEV [analytic]
- STDDEV_POP [analytic]
- STDDEV_SAMP [analytic]
- SUM [analytic]
- VAR_POP [analytic]
- VAR_SAMP [analytic]
- VARIANCE [analytic]