Window
AVG()
Overview
The AVG()
window function calculates the average (arithmetic mean) of a set of numeric values within a window. This function allows you to compute averages over a set of rows that are related to the current row, such as rows within a partition of ordered set.
Syntax
The syntax for this function is as follows:
Parameters
expression
: column or expression that the function operates on (must be of numeric type)
Example
For the needs of this section, we will use a simplified version of the film
table from the Pagila database, containing only the title
, length
and rating
columns. The complete schema for the film
table can be found on the
Pagila database website.
The query below uses the AVG()
function to calculate the rolling average of length
as rows are ordered by rating
:
By executing the above query, we will get the following output: