I have a non-repeated record column on my table that I want to access. On this record, there are several repeated values.
So it is a RECORD
, like so:
STRUCT<item ARRAY<STRING> unit_cost ARRAY<INT64> quantity ARRAY<INT64>> as costs
Eg. the data might represent:
item ['cheese', 'ham', 'salad']
unit_cost [2, 5, 8]
quantity [1, 2, 1]
So I want to return this as a nicer data structure, an array of structs:
[
{'item': 'cheese', 'unit_cost': 2, 'quantity': 1},
{'item': 'ham', 'unit_cost': 5, 'quantity': 2}
{'item': 'salad', 'unit_cost': 8, 'quantity': 1}
]
I tried:
SELECT ARRAY_AGG(costs)
but it results in
[
{
"item": ['cheese', 'ham', 'salad'],
"unit_cost": [2, 5, 8],
"quantity": [1, 2, 1]
}
]
which is not what I had expected it to return.
Is it possible to go from a STRUCT
of multiple ARRAY
to an ARRAY
of multiple STRUCT
using some clever use of Standard SQL here?