DB2 - Banco de dados - Aggregate functions - AVG
The AVG function returns the average of a set of numbers.
The schema is SYSIBM.
The data type of the result is the same as the data type of the input expression, with the following exceptions:
Table 1. Result precision and scale of the AVG aggregate function with DECIMAL input
Note:
If the data type of the result is SMALLINT, INTEGER, or BIGINT, the fractional part of the average is truncated. It is not rounded up.
During evaluation, the order in which the input values are added together is undefined, but every intermediate result must be within the range of the data type of the result.
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.
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.