Icon SUM

Returns the sum of a given numeric or interval expression for all selected rows.

Syntax
SUM([DISTINCT] <NumericExpression>|<IntervalExpression>)

<NumericExpression> =

Type of:

SMALLINT
INTEGER|INT
BIGINT
FLOAT
DECIMAL|NUMERIC

<IntervalExpression> =

Type of:

INTERVAL YEAR
INTERVAL YEAR TO MONTH
INTERVAL MONTH
INTERVAL DAY
INTERVAL DAY TO HOUR
INTERVAL DAY TO MINUTE
INTERVAL DAY TO SECOND
INTERVAL DAY TO MSECOND
INTERVAL HOUR
INTERVAL HOUR TO MINUTE
INTERVAL HOUR TO SECOND
INTERVAL HOUR TO MSECOND
INTERVAL MINUTE
INTERVAL MINUTE TO SECOND
INTERVAL MINUTE TO MSECOND
INTERVAL SECOND
INTERVAL SECOND TO MSECOND
INTERVAL MSECOND

Returns
Same as input, except for the following types:

A SMALLINT expression is promoted to an INTEGER
An INTEGER expression is promoted to a BIGINT

This is done to prevent numeric overflows

Usage
The SUM function returns the sum of a given numeric or interval expression for all selected rows. The selected rows can be grouped into logical sub-sets by using the GROUP BY clause of the SELECT statement. Any time the numeric or interval expression is NULL, it is excluded from the sum calculation.

Use the DISTINCT clause to specify that the sum calculation will only use distinct values when calculating the result.

Examples
SELECT SUM(Amount) AS TotalAmount
FROM Transactions

SELECT CAST(TransDateTime AS DATE) AS TransDate,
SUM(Amount) AS TotalAmount
FROM Transactions
GROUP BY TransDate

SQL 2003 Standard Deviations
This function deviates from the SQL 2003 standard in the following ways:

DeviationDetails
None
Image