Skip to main content
Returns the average value within the requested window. For more information on usage, please refer to Window Functions.

Syntax

AVG( <value> ) OVER ( [ PARTITION BY <partition_by> ] )

Parameters

ParameterDescriptionSupported input types
<value>A value used for the AVG() functionAny numeric type
<partition_byAn expression used for the PARTITION BY clauseAny

Return Types

  • NUMERIC if the input is type INTEGER, BIGINT or NUMERIC
  • DOUBLE PRECISION if the input is type REAL or DOUBLE PRECISION

Example

The example below shows each player’s score alongside the average score for their level. Unlike a regular AVG() aggregation, the window function keeps one row per player while also computing the per-level average.
SELECT
    nickname,
    level,
    currentscore,
    AVG(currentscore) OVER (PARTITION BY level) AS score_average
FROM
    (VALUES
        ('kennethpark', 9, 76),
        ('sabrina21', 9, 90),
        ('burchdenise', 10, 79),
        ('ymatthews', 10, 85),
        ('rileyjon', 10, 80)
    ) AS t(nickname, level, currentscore)
ORDER BY level, nickname;
nickname textlevel intcurrentscore intscore_average double null
kennethpark97683
sabrina2199083
burchdenise107981.33333333333333
rileyjon108081.33333333333333
ymatthews108581.33333333333333

Rows: 5Execution time: 2.78ms