0

I am having trouble creating a reproducible example here, because I'm not quite sure how to create a demo example table with a column of type ARRAY<STRUCT<year STRING, statCrewShirtNumber STRING>>. We have the following table:

enter image description here

And we are simply trying to turn year into its own column. We try the obvious:

select 
    customValues.year as year
from dataset.our_table

and get the error Cannot access field year on a value with type ARRAY<STRUCT<year STRING, statCrewShirtNumber STRING>>. So this is not a basic struct, but a struct inside of an array. How can we create a separate column for year?

2 Answers 2

1

You need first to unnest the array as in example below

select customValue.year as year
from `dataset.our_table` t, 
t.customValues as customValue
Sign up to request clarification or add additional context in comments.

Comments

1

Using this sample table, I also have a struct in an array as well. You can simply use UNNEST to have separate columns for each field.

enter image description here

See query below:

SELECT gender FROM `sandbox.test_table`, UNNEST(bikerides)

Result:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.