简体   繁体   English

将Google BigQuery中的表格与UNION ALL合并

[英]Merging tables in Google BigQuery with UNION ALL

I would like to do something like this in BQ. 我想在BQ做这样的事情。 The tables have different schemas. 这些表具有不同的架构。 Any ideas how to achieve this? 任何想法如何实现这一目标?

SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID, Withdrawal_this_hour FROM `tb1`
UNION ALL
SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID, Refill_this_hour FROM `tb2` 
UNION ALL
SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID, changes_this_hour, net_amount, date from `tb3`

Thanks in advance.. 提前致谢..

The columns have to be the same, so something like this: 列必须相同,因此如下所示:

SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID,
       Withdrawal_this_hour, NULL as Refill_this_hour,
       NULL as changes_this_hour, NULL as net_amount, NULL as date 
FROM `tb1`
UNION ALL
SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID,
       NULL, Refill_this_hour, NULL, NULL, NULL
FROM `tb2` 
UNION ALL
SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID,
       NULL, NULL, changes_this_hour, net_amount, date 
FROM `tb3`

For union all number of column and their data have to be same for all the tables big query docs you could try like below by using cte 对于联合,所有表的所有列数及其数据必须相同, 大型查询文档可通过使用cte尝试如下

  with cte as  (

   SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID,
   Withdrawal_this_hour, NULL as Refill_this_hour,
   NULL as changes_this_hour, NULL as net_amount, NULL as date
  UNION ALL
  SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID,
   NULL, Refill_this_hour, NULL, NULL, NULL FROM tb2
   UNION ALL
  SELECT YYYYMMDDHH, CONTAINER, Parent_Container, PROTOTYPE_ID,
   NULL, NULL, changes_this_hour, net_amount, date 
  FROM tb3
 ) select * from cte 

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

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