Skip to main content Skip to complementary content

sum

Returns the sum of numeric values in an iterative context.

The sum function returns the sum of numeric values across multiple rows in an iterative context, such as with a GROUP BY clause or in a FROM-SELECT query.

To calculate the sum of values in an array or collection, use the arraySum function instead.

Arguments

Expression returning a numeric value.

Examples

Calculate the total price across all orders:
FROM order
SELECT sum(price)
Calculate the total quantity per order:
FROM order UNNEST items
GROUP BY order_id
SELECT {
  order_id,
  total_quantity = sum(quantity)
}

Related functions

  • arraySum - Calculates the sum of values in an array
  • avg - Calculates the average in an iterative context

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – please let us know!