DB2 Version 9.7 for Linux, UNIX, and Windows

AVG aggregate function

Read syntax diagramSkip visual syntax diagram
           .-ALL------.                  
>>-AVG--(--+----------+--expression--)-------------------------><
           '-DISTINCT-'                  

The schema is SYSIBM.

The AVG function returns the average of a set of numbers.

The argument values must be numbers (built-in types only) and their sum must be within the range of the data type of the result, except for a decimal result data type. For decimal results, their sum must be within the range supported by a decimal data type having a precision of 31 and a scale identical to the scale of the argument values. The result can be null.

The data type of the result is the same as the data type of the argument values, except that:
  • The result is a large integer if the argument values are small integers.
  • The result is double-precision floating point if the argument values are single-precision floating point.
  • The result is DECFLOAT(34) if the argument is DECFLOAT(n).

If the data type of the argument values is decimal with precision p and scale s, the precision of the result is 31 and the scale is 31-p+s.

The function is applied to the set of values derived from the argument values by the elimination of null values. If DISTINCT is specified, redundant duplicate values are eliminated. When interpreting the DISTINCT clause for decimal floating-point values that are numerically equal, the number of significant digits in the value is not considered. For example, the decimal floating-point number 123.00 is not distinct from the decimal floating-point number 123. The representation of the number returned from the query will be any one of the representations encountered (for example, either 123.00 or 123).

If the function is applied to an empty set, the result is a null value. Otherwise, the result is the average value of the set.

The order in which the values are added is undefined, but every intermediate result must be within the range of the result data type.

If the type of the result is integer, the fractional part of the average is lost.

Examples: