I have 12 tables where I need to combine 5 of the columns into a new table.
Each table is a different month but they all have the same column names.
I believe I need to use 11 join's, but when I use left join, I'm returning data only from the first table(jan). When I use full outer join I am returning rows full of Null's but looks like merged tables. I am using BigQuery.
SELECT
jan.ride_id,
jan.member_casual,
jan.ride_length,
jan.day_of_week,
jan.rideable_type
FROM
`jan_22` as jan
left join `feb_22` as feb
on jan.ride_id= feb.ride_id
The result I'm looking for is the 5 columns with data from all 12 tables merged. Just trying to merge the first 2 correctly before adding the others.
Also should I use select into or insert into for creating a new table with the 5 columns?
Thanks for your help.
As suggested by Mikhail Berlyant in the comments you should use union
as the difference between join
and union
is that join
joins a table horizontally and union
joins vertically. And as you need 5 columns you should join them using union
as follows:
insert dataset.new_table(<the columns(6)>)
SELECT
'Jan' as Month_Name,
jan.ride_id,
jan.member_casual,
jan.ride_length,
jan.day_of_week,
jan.rideable_type
FROM
`jan_22` as jan
UNION ALL
SELECT
'Feb',
feb.ride_id,
feb.member_casual,
feb.ride_length,
feb.day_of_week,
feb.rideable_type
FROM `feb_22` as feb
...
And as you probably have rows that return duplicate values it will be ignored by union
but not by union all
so you should use union all
.
See here in docs how to insert
into a table using select
statement.
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.