Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 449
sandeep0101 Avatar asked Mar 12 '23 13:03

sandeep0101


2 Answers

You are looking for union all:

select bu, campaign, impressions, clicks
from table1
union all
select bu, campaign, total_impressions, total_clicks
from table2;
like image 192
Gordon Linoff Avatar answered Mar 21 '23 06:03

Gordon Linoff


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.

like image 27
Mark Avatar answered Mar 21 '23 08:03

Mark