google-bigquerynested-fields

BigQuery - ARRAY_ARG and UNNEST on the same pass when one element is needed


I was wondering which is the fastest way to unnest an array generated with ARRAY_AGG in BigQuery, assuming I want to take only one element over the partition of the array. Let me try to explain it better

Let's say I have this:

with (
select a, ARRAY_AGG(struct(time, value) order by time) as b
group by a
) tbl

And let's say I just want to pick one element of the array, specifically the last element of the array (the one with the most recent time, since the array is ordered). Ie, ending with this (where time and value assumes the values of the last element of the array, as I said above):

a | time | value
--+------+------

Of course, I could unnest(b) and create a partition, but this would mean an additional pass into the table.

Is there a way to do that in a single pass instead?


Solution

  • Try offset:

    ARRAY_AGG(struct(time, value) order by time desc limit 1)[offset(0)].*