Reputation: 463
Assume the below table that has two repeated fields (product1 & product2).
What is the most efficient way to left outer join (on sku) the two repeated fields into one repeated field so that the result looks like this?
Upvotes: 0
Views: 261
Reputation: 172964
Consider below approach
select * except(product1, product2),
array(
select as struct sku, description, quantity
from t.product1
left join t.product2
using(sku)
) product
from data t
If applied to sample data in your question - output is
Upvotes: 1
Reputation: 1269513
Hmmm . . . You can unnest()
and then reaggregate:
select t.*,
(select array_agg(product)
from (select sku, p2.description, p2.quantity
from unnest(product1) p1 left join
unnest(product2) p2
using (sku)
) product
) as product
from t;
Upvotes: 1