Syntax
Parameters
| Parameter | Description | Supported input types |
|---|---|---|
<value> | The expression used to calculate the average | Any numeric type |
<condition> | An optional boolean expression to filter rows used in aggregation | BOOL |
AVG() aggregate function ignores rows with NULL values. For example, an AVG from 3 rows containing 1, 2, and NULL returns 1.5 because the NULL row is not counted. To calculate an average that includes NULL, use SUM(COLUMN)/COUNT(*).
Return Types
NUMERICif the input is typeINTEGER,BIGINTorNUMERICDOUBLE PRECISIONif the input is typeREALorDOUBLE PRECISION
Precision and Determinism
ApplyingAVG to REAL and DOUBLE PRECISION is subject to floating point arithmetic accuracy limitations and its resulting error.
This error may add up when aggregating multiple values.
The order of operations while computing the aggregate is non-deterministic.
This can lead to varying total floating point error when running a query multiple times.
If this is not acceptable for your use-case, aggregate on NUMERIC data instead.
Example
The example below uses the following tableLevelPoints. This table includes the maximum points a player can score at each level of the game:
| levels | maxpoints |
|---|---|
| 1 | 50 |
| 2 | 100 |
| 3 | 150 |
| 4 | 200 |
| 5 | 250 |
maxpoints value.
| AverageMaxPoints |
|---|
| 150 |