sqlgoogle-bigqueryunnest

Unnest in BigQuery leading to "Array" error


I am taking over the work of a former colleague and try to build on top of it. His work led to unnesting a parent categories, and I am trying to achieve the same for another category. My additional Unnesting is, however, leading to an error due to Array structure - which is why I want to unnest in the first play right? :(

Please see attached image Picture

(the Error code is : "Cannot access field refund_line_items on a value with type ARRAY>, id INT64, processed_at TIMESTAMP, ...>> at [19:16]")

Any help will be much appreciated!


Solution

  • If I understand your table schema correctly. refunds is an array, so you need to unnest the array field then access its struct field inside, like

    SELECT refund.refund_line_items
    FROM orders
    JOIN UNNEST(refunds) refund
    

    See more on: Querying STRUCT elements in an ARRAY