SUM

Calculates the sum of an expression.

Syntax

SUM ([ DISTINCT ] <value>)

Parameters

Parameter Description Supported input types
<value> The expression used to calculate the sum. Any numeric type

Valid values for <value> include column names or expressions that evaluate to numeric values. When DISTINCT is being used, only the unique number of rows with no NULL values are summed.

Return Types

NUMERIC

Precision and Determinism

Applying SUM to REAL and DOUBLE PRECISION is subject to floating point arithmetic accuracy limitations and its resulting error. This error may add up when aggregating multiple values.

The order of operations while computing the aggregate is non-deterministic. This can lead to varying total floating point error when running a query multiple times. If this is not acceptable for your use-case, aggregate on NUMERIC data instead.

Example

For this example, see the following table tournaments:

name totalprizedollars
The Drifting Thunderdome 24,768
The Lost Track Showdown 5,336
The Acceleration Championship 19,274
The Winter Wilderness Rally 21,560
The Circuit Championship 9,739
The Singapore Grand Prix 19,274
SELECT
	SUM(totalprizedollars)
FROM
	tournaments

Returns: 99,951

SELECT
	SUM (DISTINCT totalprizedollars)
FROM
	tournaments

For this calculation, since both the Singapore Grand Prix and The Acceleration Championship have the same total prize of 19,274, only one of these values in this sum in included.

Returns: 80,677