Previous Topic

Next Topic

SUM - SQL Function

SUM {Expression}

The SUM function calculates the sum of values for a field.

Use SUM to sum all the values in the specified field. As an aggregate function, SUM performs its calculation aggregating values in the same field across all rows in a dataset. The dataset can be the entire table, a filtered dataset, or a logical group produced by a GROUP BY clause. Field values of zero are included in the aggregation. NULL field values are not counted in the calculation. If the number of qualifying rows is zero, SUM returns a NULL value.

SUM function syntax has these named arguments:

Parameter

Description

Expression

Required. The field reference or expression to calculate the sum total.

For example:

SELECT SUM(GrossAmount) from INHEAD

When used with a GROUP BY clause, SUM returns one calculation value for each group. This value is the aggregation of the specified field for all rows in each group. The following example aggregates the total value for the GrossAmount field in the INHEAD table, producing a subtotal for each Customer in the ARCUST table:

SELECT C.CustomerCode , SUM(H.GrossAmount) as Total

FROM ARCUST C, INHEAD H

WHERE C.CustomerCode = H.CustomerCode

GROUP BY C.CustomerCode

ORDER BY C.CustomerCode

See Also

SQL Aggregate Functions

Book Contents

Book Index