The AVG function returns the average of a set of numbers.
The schema is SYSIBM.
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).
The result can be null. 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.
SELECT AVG(PRSTAFF)
INTO :AVERAGE
FROM PROJECT
WHERE DEPTNO = 'D11'
Results in AVERAGE being set to 4.25 (that is 17/4) when using the sample table.
SELECT AVG(DISTINCT PRSTAFF)
INTO :ANY_CALC
FROM PROJECT
WHERE DEPTNO = 'D11'
Results in ANY_CALC being set to 4.66 (that is 14/3) when using the sample table.