简体   繁体   中英

bigquery merging same columns from different tables

I have 2 tables in BigQuery and I want to merge their columns together and stack the data to get 1 big table with all the data. Effectively, the tables contain same data, but few columns have different names, while few have same names.

Below is an example of how data exists in these tables:

Table1:

Date     | BU  | Campaign | Impressions | Clicks
01/01/15 | XYZ |  C1      |    500      |   20

Table2:

Date     | BU  | Campaign | Total_Impressions | Total_Clicks
01/01/16 | ABC |  C2      |    600            | 30

Expected output:

Table3:

Date     | BU  | Campaign | Impressions | Clicks
01/01/15 | XYZ |  C1      |    500      |   20
01/01/16 | ABC |  C2      |    600      |   30

How can I do this in BigQuery?

By default BigQuery still uses its own legacy SQL dialect. There you can UNION multiple tables with a comma as explained in the reference .

For it to work the columns from each table first need to get the same name.

So the query then becomes:

SELECT *
FROM 
  (select bu, campaign, impressions, clicks from table1),
  (select bu, campaign, total_impressions AS impressions, total_clicks AS clicks from table2)

When using the new standard SQL instead of the legacy dialect, you can use a union all statement.

You are looking for union all :

select bu, campaign, impressions, clicks
from table1
union all
select bu, campaign, total_impressions, total_clicks
from table2;

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM