Constructing nested array structs in BigQuery
with animal as
(
select * from
unnest([struct
(1 as zoo_id, 'Horse' AS animal_name, 1 as animal_count),
(1, 'Cow', 25),
(2, 'Old World Swallowtail Butterfly', 1000 ),
(4, 'Fish' ,5500),
(5, 'Beetle' ,700000)
])
)
, zoo as
(
select * from
unnest([struct
(1 as zoo_id, 'Skansen' as zoo_name, 100 as city_id),
(2, 'Fjärilshuset', 100 ),
(3, 'Zoologisk Have', 101 ),
(4, 'Särkänniemi Aquarium', 102),
(5, 'Kew Gardens', 103)
])
)
, city as
(
select * from
unnest([struct
(100 as city_id, 'Stockholm' as city_name),
(101, 'København' ),
(102, 'Tampere'),
(103, 'London')
])
)
select
c.*,
array( select as struct
zoo_id,
zoo_name,
array( select as struct
animal_name,
animal_count
from animal a where a.zoo_id = z.zoo_id ) as animal
from zoo z where z.city_id=c.city_id ) as zoo,
from city c
Comments