Search code examples
google-bigquerynestedunion-all

BigQuery: Union on repreated fields with different order of fields


How to make a UNION ALL work for repeated fields if the order of the fields does not match?

In the example below I try to UNION data_1_nested and data_2_nested, while the repeated field nested has two fields: id and age but in different order.

I could UNNEST and renest but this would not be very helpful if I have more then 1 nested field that I need to UNION on.

Example:

with 
data_1 as (
Select 'a123' as id, 1 as age, 'a' as grade
union all 
Select 'a123' as id, 3 as age,'b' as grade
union all 
Select 'a123' as id, 4.5 as age,'c' as grade
)
,
data_2 as (
Select 'b456' as id, 6 as age,'e' as grade
union all 
Select 'b456' as id, 5 as age,'f' as grade
union all 
Select 'b456' as id, 2.5 as age,'g' as grade
)
,
data_1_nested as (
SELECT id, 
       array_agg(STRUCT(
                      age,grade
                        ))  as nested
from data_1                      
group by 1
)
,
data_2_nested as (
SELECT id, 
       array_agg(STRUCT(
                      grade, age
                        ))  as nested
from data_2                      
group by 1
)


SELECT * from data_1_nested
union all 
SELECT * from data_2_nested

Solution

  • Below should work for you

    select * from data_1_nested
    union all 
    select id, array(select as struct age, grade from t.nested) from data_2_nested t   
    

    if applied to sample data from your question - output is

    enter image description here