SUM

Applies to: ✅ Data 360 SQL ✅ Tableau Hyper API

Computes the sum of all input values.

Syntax 

1sum(<expression>)

Arguments 

Required 

  • <expression>: An expression of any numerical type.

Returns 

  • bigint for integer-type arguments.
  • NUMERIC(38,s) for NUMERIC(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 COALESCE to substitute zero for NULL if necessary.

Examples 

Calculate Sum 

Get the total revenue.

1SELECT sum(amount) AS total_revenue
2FROM sales;

Sum by Group 

Calculate totals per group.

1SELECT category, sum(quantity) AS total_quantity
2FROM orders
3GROUP BY category;

Handle Empty Results 

Use COALESCE to return zero for empty sets.

1SELECT COALESCE(sum(amount), 0) AS total
2FROM orders
3WHERE order_date > '2024-12-31';

Related Documentation