DB2- AVG Column function – Best example

The AVG function computes the average of the values for the column or expression specified as an argument. We should not give AVG function on CHAR arguments.

This function operates only on numeric arguments. The following example calculates the average salary of each department:

SELECT WORKDEPT, AVG(SALARY)
FROM DSN81010.EMP
GROUP BY WORKDEPT;

  • The AVG function is the preferred method of calculating the average of a group of values.
  • Although an average, in theory, is nothing more than a sum divided by a count, DB2 may not return equivalent values for AVG(COL_NAME) and SUM(COL_NAME)/COUNT(*).

The reason is that the COUNT function will count all rows regardless of value, whereas SUM ignores nulls.

Advertisements

Author: Srini

Experienced software developer. Skills in Development, Coding, Testing and Debugging. Good Data analytic skills (Data Warehousing and BI). Also skills in Mainframe.