I have about 3 different tables in the same BigQuery dataset, and they all have the same schema. I need to compare all there tables for the presence of multiple values, and have the results listed in separate columns.
For example: Tables: 'Luxury', 'Economy' and 'SUVs'. They all have columns for 'color', 'price' and 'size'.
I want to query all the tables to get the tables where color = 'red' or color = 'blue'.
This should only return two rows - like:
Luxury | Economy | SUVs |
---|---|---|
red | red | red |
blue |
So in this case, Economy table has red and blue cars, but Luxury and SUVs have only red cars. I have tried, UNION ALL, and INNER JOIN, but they return only one row
Thank you
Consider below approach
select * except(pos) from (
select * from (
select distinct color, 'Luxury' source, row_number() over(order by color) pos from Luxury union all
select distinct color, 'Economy' source, row_number() over(order by color) pos from Economy union all
select distinct color, 'SUVs' source, row_number() over(order by color) pos from SUVs
)
where color in ('red', 'blue')
)
pivot (any_value(color) for source in ('Luxury', 'Economy', 'SUVs'))
with output like below
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.