简体   繁体   English

bigquery合并来自不同表的相同列

[英]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. 我在BigQuery中有2个表,我想将它们的列合并在一起并堆叠数据以获取包含所有数据的1个大表。 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: 表格1:

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

Table2: 表2:

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

Expected output: 预期产量:

Table3: 表3:

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? 如何在BigQuery中执行此操作?

By default BigQuery still uses its own legacy SQL dialect. 默认情况下,BigQuery仍使用其自己的旧版SQL方言。 There you can UNION multiple tables with a comma as explained in the reference . 如参考中所述,您可以其中用逗号UNION多个表。

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. 当使用新的标准SQL而不是传统方言时,可以使用union all语句。

You are looking for union all : 您正在寻找union allunion all

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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