Aggregates
Aggregate
sum()
Calculates the sum value for all tuples in arg.
Reference: Haybarn WASM 2.0.0-rc12
Arguments & returns
8 overloadssum(arg)
Aggregate
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.
Haybarn WASM 2.0.0-rc12 · In your browser
Guide example · Run to view results.
| total | empty_total |
|---|---|
5 | NULL |
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
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.
Haybarn WASM 2.0.0-rc12 · In your browser
Guide recipe · Run to view results.
In other engines
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'))
SELECT * FROM produce PIVOT(SUM(sales) FOR quarter IN ('Q1' AS "'Q1'", 'Q2' AS "'Q2'"))