sum
On this page
Returns the sum of non-NULL values.
Syntax
sum(<expr>)Arguments
| Argument | Type | Description |
|---|---|---|
<expr> | int, uint, or float | Input value. |
Returns
Returns the same numeric type as the input. Returns NULL for empty input or when every value is NULL.
Examples
Payment events use positive amounts for captures and negative amounts for refunds. Sum the signed amounts to calculate net sales by store.
VALUES
('north', 'captured', 12000), ('north', 'captured', 8000),
('north', 'refunded', -3000), ('south', 'captured', 4500)
SELECT $0 AS store, $1 AS event, $2 AS amount_cents
GROUP BY store
AGGREGATE sum(amount_cents) AS net_sales_cents
ORDER BY store+-------+-----------------+
| store | net_sales_cents |
+-------+-----------------+
| north | 17000 |
| south | 4500 |
+-------+-----------------+