BigQuery Standard SQL中具有不同架构的联合表

时间:2018-11-18 16:27:44

标签: sql google-bigquery

用不同的架构附加表的最佳方法是什么?

表0模式

Table0 Schema

Table0 Preview

表1架构

Table1 Schema

Table1 Preview


这很好

SELECT img1, NULL as img2 FROM `xxx.staging.table0` 
UNION ALL 
SELECT img1, img2 FROM `xxx.staging.table1` 

但是有没有附加表而不必显式声明每一列的方法?

将表0另存为

也是一种好习惯
SELECT img1, CAST(NULL as string) as img2 FROM `modemutti-8d8a6.staging.table0` 

确保img2是字符串:

enter image description here

然后在两个表之间附加完全相同的架构?

SELECT * FROM `xxx.staging.table1ab` 
UNION ALL 
SELECT * FROM `xxx.staging.table1` 

1 个答案:

答案 0 :(得分:1)

以下是用于BigQuery标准SQL

#standardSQL
SELECT * FROM `project.dataset.table1`
UNION ALL
SELECT * FROM `project.dataset.table0`
LEFT JOIN (SELECT '', '') ON FALSE

注意:在(SELECT '', '')中,''的数量与表1中额外字段的数量一样多-因此,两个表中的字段总数相同

您可以使用以下虚拟数据进行测试,玩

#standardSQL
WITH `project.dataset.table0` AS (
  SELECT 'aaa' img1 UNION ALL
  SELECT 'xxx' UNION ALL
  SELECT 'yyy' UNION ALL
  SELECT 'zzz' 
),  `project.dataset.table1` AS (
  SELECT 'bbb' img1, 'ccc' img2, 'ddd' img3 UNION ALL
  SELECT 'eee', 'fff', 'ggg'
)
SELECT * FROM `project.dataset.table1`
UNION ALL
SELECT * FROM `project.dataset.table0`
LEFT JOIN (SELECT '', '') ON FALSE

有结果

Row     img1    img2    img3     
1       bbb     ccc     ddd  
2       eee     fff     ggg  
3       aaa     null    null     
4       xxx     null    null     
5       yyy     null    null     
6       zzz     null    null