SUM
Returns the sum of all non NULL
values in a column or grouping of columns.
Use the TOTAL function to sum floating point numbers or long values.
Syntax
SELECT SUM(columnExpression) FROM <multipartIdentifier>
[ GROUP BY groupByColumn ];
Arguments
columnExpression
A column or expression.
If all the values in the columnExpression are
NULL
thenSUM
returnsNULL
.
SUM
will throw an "integer overflow" exception if an integer overflow occurs at any point during the computation. The [TOTAL
]](/docs/language-spec/functions/aggregate/total) function never throws an integer overflow.
groupByColumn
A column or columns used to perform summary or aggregate operations against. The GROUP BY
clause returns one row for each column grouping.
Return Value(s)
Returns an integer representing the sum of the columnExpression if all non NULL
inputs are integers. If any inputs are not integers or are NULL
then a floating point value which might be an approximation to the true sum is returned.
Examples
Return the sum of a column expression over an entire resource
SELECT sum(json_array_length(disks)) as sum_disks
FROM google.compute.instances
WHERE project = 'stackql-demo'
AND zone = 'australia-southeast1-a';
Return the sum of a column expression grouped by another column expression
SELECT tags, sum(json_array_length(disks)) as sum_disks
FROM google.compute.instances
WHERE project = 'stackql-demo'
AND zone = 'australia-southeast1-a'
GROUP BY json_extract(tags, '$.instanceType');
For more information, see https://www.sqlite.org/lang_aggfunc.html#sumunc.