Appearance
AVG
Return the average (arithmetic mean) of the values for an expression.
AVG | AVERAGE (expression)
AVG
and AVERAGE
are both supported. The expression must be numeric data type or a time data type.
For example:
premdb=# SELECT AVG(capacity)::INT FROM team WHERE capacity >0;
avg
-------
31895
(1 row)
The following example groups by the city
column to return average stadium capacities per city:
premdb=# SELECT city, AVG(capacity)::INT
FROM team WHERE capacity >0 GROUP BY city ORDER BY 2 DESC;
city | avg
---------------+-------
Manchester | 65366
Newcastle | 52405
Sunderland | 49000
Liverpool | 42482
Leeds | 39460
Sheffield | 36217
...
Return Types
The return type of the AVG function depends on the input type:
AVG(INT2)
returnsDECIMAL(25,6)
AVG(INT4)
returnsDECIMAL(25,6)
AVG(INT8)
returnsDECIMAL(38,6)
AVG(FLOAT4)
returnsFLOAT4
AVG(FLOAT8)
returnsFLOAT8
AVG(DECIMAL(p,s))
returnsDECIMAL(p,s)
AVG(TIME)
returnsINTERVAL
Parent topic:Aggregate Functions