ARRAY_SUM
Function
The ARRAY_SUM
function calculates the sum of all numeric elements in an array.
Syntax
ARRAY_SUM(ARRAY<T>)
Parameters
ARRAY<T>
: An array containing numeric type elements.
Return Value
-
Returns the sum of all non-
NULL
elements in the array.- If all elements are
NULL
, returnsNULL
.
- If all elements are
Usage Notes
-
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.
Examples
-
Simple example
SELECT ARRAY_SUM([1, 2, 3, 4]);
+-------------------------+
| ARRAY_SUM([1, 2, 3, 4]) |
+-------------------------+
| 10 |
+-------------------------+ -
Handling
NULL
values in arraysSELECT 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 |
+-------------------------+