The ARRAY_SUM function calculates the sum of all numeric elements in an array.
ARRAY_SUM(ARRAY<T>)
ARRAY<T>: An array containing numeric type elements.
Returns the sum of all non-NULL elements in the array.
NULL, returns NULL.Summation of elements uses the + operator.
Elements that are NULL are automatically ignored.
If the array contains non-numeric type elements (such as strings), it will result in a runtime error.
Simple example
SELECT ARRAY_SUM([1, 2, 3, 4]); +-------------------------+ | ARRAY_SUM([1, 2, 3, 4]) | +-------------------------+ | 10 | +-------------------------+
Handling NULL values in arrays
SELECT ARRAY_SUM([1, NULL, 3]); +-------------------------+ | ARRAY_SUM([1, NULL, 3]) | +-------------------------+ | 4 | +-------------------------+ SELECT ARRAY_SUM(NULL); +-----------------+ | ARRAY_SUM(NULL) | +-----------------+ | NULL | +-----------------+ SELECT ARRAY_SUM([NULL, NULL]); +-------------------------+ | ARRAY_SUM([NULL, NULL]) | +-------------------------+ | NULL | +-------------------------+