I have a column in a snowflake table, each row in the column has a array of floats of 360 elements. I want to write a snowflake query, so that I get an aggregate of all the elements in the array, such that the out is 360 values. All the element 1 are summed, all the element at index 2 are summed and so on. The result of the query is a single array of 360 values.
Any idea how I can achieve that in most efficient way, as I need to sum a million records in one query.
Let's assume that your table is called ARRAY_DEMO and your arrays are in a column called ARRAY1. The following SQL should do what you want:
SELECT
ARRAY_AGG(sum_val::integer) WITHIN GROUP (ORDER BY index ASC)
FROM (
SELECT f.index, sum(f.value) sum_val
FROM array_demo ad,
lateral flatten(input => ad.array1) f
GROUP BY f.index
)
;
You may need to adjust the "::integer" depending on the type of data you have in your array