Aggregates Aggregate

sum()

Calculates the sum value for all tuples in arg.

Reference: Haybarn WASM 2.0.0-rc12

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

Add values across rows

Add numeric values within a group, ignoring NULL inputs. Use GROUP BY to produce a separate total for each group.

An empty input or a group containing only NULL values produces NULL, rather than zero.

Add values across rows
SQL
Haybarn WASM 2.0.0-rc12 · In your browser

Guide example · Run to view results.

Expected result
totalempty_total
5NULL

Watch for: COALESCE(sum(value), 0) is useful when an empty total should be zero. Avoid using sum(DISTINCT value) to repair duplicated join rows.

Example

sum example
SQL
Haybarn WASM 2.0.0-rc12 · 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 2.0.0-rc12 · In your browser

Guide recipe · Run to view results.

In other engines

BigQuerysum()

SELECT SUM(x IGNORE NULLS) AS x
In Haybarn
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 Haybarn
SELECT * FROM produce PIVOT(SUM(sales) FOR quarter IN ('Q1' AS "'Q1'", 'Q2' AS "'Q2'"))

SQLGlot example ↗