Aggregates Aggregate

sum()

Calculates the sum value for all tuples in arg.

Arguments & returns

8 overloads

sum(arg)

Aggregate
Overload link arg Returns
#01 BIGINT HUGEINT
#02 BIGNUM BIGNUM
#03 BOOLEAN HUGEINT
#04 DECIMAL DECIMAL
#05 DOUBLE DOUBLE
#06 HUGEINT HUGEINT
#07 INTEGER HUGEINT
#08 SMALLINT HUGEINT

Example

sum example
SQL
Haybarn WASM 1.5.5-rc3 · In your browser

Engine example with sample data · Run to view results.

In a query

Give your timestamps a rhythm.

Group events into calendar months with date_trunc. The same pattern works for days, weeks, and years.

Give your timestamps a rhythm.
SQL
Haybarn WASM 1.5.5-rc3 · In your browser

Guide recipe · Run to view results.

In other engines

BigQuerysum()

SELECT SUM(x IGNORE NULLS) AS x
In DuckDB
SELECT SUM(x) AS x

SQLGlot example ↗

Used in larger rewrites 1

These examples use sum as one part of a larger SQL translation.

Snowflake

SELECT * FROM produce PIVOT(SUM(produce.sales) FOR produce.quarter IN ('Q1', 'Q2'))
In DuckDB
SELECT * FROM produce PIVOT(SUM(sales) FOR quarter IN ('Q1' AS "'Q1'", 'Q2' AS "'Q2'"))

SQLGlot example ↗