An aggregate function (formerly known as a column function) accepts arguments and returns a single scalar value that is the result of an evaluation of a set of like values, such as those in a column within a set of one or more rows.
The argument of an aggregate function is a set of values derived from an expression. The expression can include columns, but cannot include a scalar-fullselect, another aggregate function, or an XMLQUERY or XMLEXISTS expression (SQLSTATE 42607). The scope of the set is a group or an intermediate result table.
If a GROUP BY clause is specified in a query, and the intermediate result of the FROM, WHERE, GROUP BY, and HAVING clauses is the empty set, the aggregate functions are not applied; the result of the query is the empty set; the SQLCODE is set to +100; and the SQLSTATE is set to '02000'.
If a GROUP BY clause is not specified in a query, and the intermediate result of the FROM, WHERE, and HAVING clauses is the empty set, the aggregate functions are applied to the empty set.
SELECT COUNT(DISTINCT JOBCODE) FROM CORPDATA.EMPLOYEE WHERE WORKDEPT = 'D01'
The keyword DISTINCT is not considered to be an argument of the function, but rather a specification of an operation that is performed before the function is applied. If DISTINCT is specified, 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 ALL is implicitly or explicitly specified, duplicate values are not eliminated.
For compatibility with other SQL implementations, UNIQUE can be specified as a synonym for DISTINCT in aggregate functions.
SELECT MAX(BONUS + 1000) INTO :TOP_SALESREP_BONUS FROM EMPLOYEE WHERE COMM > 5000
Aggregate functions can be qualified with a schema name (for example, SYSIBM.COUNT(*)).