Bigquery将两个不同结构的数组联合

时间:2018-07-03 14:08:30

标签: google-bigquery

我有两个桌子

v1 ARRAY<STRUCT<a int64>>

v2 ARRAY<STRUCT<a int64, b int64>>

我想编写一个查询,该查询使用union all合并两个表,并且对于v1行,将空值替换为b字段。任何帮助表示赞赏:)

我正在使用标准SQL。

1 个答案:

答案 0 :(得分:2)

以下是用于BigQuery标准SQL

#standardSQL
SELECT 
  ARRAY(SELECT AS STRUCT val.a, NULL AS b FROM UNNEST(arr1) val) arr
FROM `project.dataset.v1` 
UNION ALL 
SELECT arr2 AS arr
FROM `project.dataset.v2` 

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

#standardSQL
WITH `project.dataset.v1` AS (
  SELECT [STRUCT<a INT64>(1),STRUCT(2),STRUCT(3)] arr1
), `project.dataset.v2` AS (
  SELECT [STRUCT<a INT64, b INT64>(100, 1),STRUCT(100, 2),STRUCT(100, 3)] arr2
)
SELECT 
  ARRAY(SELECT AS STRUCT val.a, NULL AS b FROM UNNEST(arr1) val) arr
FROM `project.dataset.v1` 
UNION ALL 
SELECT arr2 AS arr
FROM `project.dataset.v2`  

结果为

Row arr.a   arr.b    
1   1       null     
    2       null     
    3       null     
2   100     1    
    100     2    
    100     3