Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API
Computes the sum of all input values.
Syntax
sum(<expression>)
Arguments
Required
<expression>: An expression of any numerical type.
Returns
bigintfor integer-type arguments.NUMERIC(38,s)forNUMERIC(p,s)arguments.
Returns NULL if no rows are selected.
Considerations
- NULL values are ignored in the calculation.
- Returns NULL when applied to an empty set.
- Use
COALESCEto substitute zero for NULL if necessary.
Examples
Calculate Sum
Get the total revenue.
SELECT sum(amount) AS total_revenue
FROM sales;
Sum by Group
Calculate totals per group.
SELECT category, sum(quantity) AS total_quantity
FROM orders
GROUP BY category;
Handle Empty Results
Use COALESCE to return zero for empty sets.
SELECT coalesce(sum(amount), 0) AS total
FROM orders
WHERE order_date > '2024-12-31';