- Categories:
Aggregate functions (General) , Window functions (General, Window Frame)
AVG¶
Returns the average of non-NULL records. If all records inside a group are NULL, the function returns NULL.
Syntax¶
Aggregate function
Window function
For detailed window_frame syntax, see Window function syntax and usage.
Arguments¶
expr1This is an expression that evaluates to a numeric data type (INTEGER, FLOAT, DECIMAL, etc.).
expr2This is the optional expression to partition by.
expr3This is the optional expression to order by within each partition.
Usage notes¶
When passed a VARCHAR expression, this function implicitly casts the input to floating point values. If the cast cannot be performed, an error is returned.
When this function is called as a window function with an OVER clause that contains an ORDER BY clause:
A window frame is required. If no window frame is specified explicitly, the following implied window frame is used:
RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROWFor more information about window frames, including syntax, usage notes, and examples, see Window function syntax and usage.
Using the keyword DISTINCT inside the window function is prohibited and results in a compile-time error.
Examples¶
Setup:
Show the data:
Calculate the average of the columns that are numeric or that can be converted to numbers:
Combine AVG with GROUP BY to calculate the averages of different groups:
Use as a simple window function: