In BigQuery, I have the following query:
SELECT
`order`.source AS order_source,
`order`.key AS order_key,
ANY_VALUE(`order`.date) AS order_date,
order_product.key AS key,
ANY_VALUE(order_product.sku) AS sku,
ANY_VALUE(order_product.name) AS name,
ANY_VALUE(order_product.quantity) AS quantity,
ANY_VALUE(order_product.subtotal) AS revenue,
ARRAY_CONCAT_AGG(moved_quants) AS moved_quants
FROM
`analytics.spr.stock_move_shipments`
GROUP BY
`order`.source,
`order`.key,
order_product.key
Each row in analytics.spr.stock_move_shipments has repeated moved_quants field. As you can see in the query, I am grouping rows together and creating an array of moved_quants for the group using ARRAY_CONCAT_AGG.
However, what I would really like to do is perform some aggregation on those moved_quants and display those results in each row. I thought I could just change the last line of the SELECT statement to something like this:
SELECT
`order`.source AS order_source,
`order`.key AS order_key,
ANY_VALUE(`order`.date) AS order_date,
order_product.key AS key,
ANY_VALUE(order_product.sku) AS sku,
ANY_VALUE(order_product.name) AS name,
ANY_VALUE(order_product.quantity) AS quantity,
ANY_VALUE(order_product.subtotal) AS revenue,
(SELECT SUM(t1.inventory_value) FROM UNNEST(ARRAY_CONCAT_AGG(moved_quants)) t1) AS inventory_value
FROM
`analytics.spr.stock_move_shipments`
GROUP BY
`order`.source,
`order`.key,
order_product.key
However, I am receiving the following error:
Aggregate function ARRAY_CONCAT_AGG not allowed in UNNEST
Why is this not allowed? Shouldn't I be able to simply UNNEST the array created by ARRAY_CONCAT_AGG? Is there some better way to accomplish this?