SUM aggregate function

The SUM function returns the sum of a set of numbers.

Note: The result of the function can be affected by the enablement of the large_aggregation configuration parameter.
Read syntax diagramSkip visual syntax diagramSUM(ALLDISTINCTexpression)

The schema is SYSIBM.

expression
An expression that returns a value of any built-in numeric data type.
The data type of the result is the same as the data type of the input expression, with the following exceptions:
  • If the data type of the input expression is SMALLINT or INTEGER, the data type of the result is BIGINT.
  • If the data type of the input expression is single-precision floating point (REAL), the data type of the result is double-precision floating point (DOUBLE).
  • If the data type of the input expression is DECFLOAT(n), the data type of the result is DECFLOAT(34).
  • If the input expression is a DECIMAL value with precision p and scale s, the result is a DECIMAL value with precision 31 and scale 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 also 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 sum of the values in the set.

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

Example

Using the EMPLOYEE table, set the host variable JOB_BONUS (decimal(9,2)) to the total bonus (BONUS) paid to clerks (JOB='CLERK').
   SELECT SUM(BONUS)
     INTO :JOB_BONUS
     FROM EMPLOYEE
     WHERE JOB = 'CLERK'
Results in JOB_BONUS being set to 2800 when using the sample table.