Search code examples
arraysgoogle-cloud-platformgoogle-bigquerysumaggregate-functions

Sum of repeated numeric columns (array) in Bigquery


How can I get the sum of a repeated numeric column (array) in BigQuery ?

For example, from the following array:

SELECT [4, 5, 9] as repeated_numeric
UNION ALL SELECT [2,3];
Row repeated_numeric
1 [4, 5, 9]
2 [2,3]

I am looking for this result:

Row sum
1 18
2 5

The length of my arrays is not fixed.


Solution

  • Consider below simple approach

    select repeated_numeric, 
      (select sum(num) from t.repeated_numeric num) total
    from your_table t           
    

    if applied to sample data in your question - output is

    enter image description here