交叉连接未连接的空数组

时间:2018-05-04 15:53:14

标签: google-cloud-platform google-bigquery

我有一个派生自JSON模式的表,如下所示:

{name: 'Foo', implies: []}
{name: 'Bar', implies: ['Foo']}
{name: 'Baz', implies: ['Foo', 'Bar']}

我想删除implies字段,以便每个值只有一个name。例如:

name, implies
Foo,
Bar, Foo
Baz, Foo
Baz, Bar 

我的查询是:

SELECT name, implies FROM table, UNNEST(implies) AS implies

但是带有空暗示的名字会被删除,所以我没有得到名字“Foo”的字段。

我无法在FULL JOIN上执行UNNEST(implies)

 Array scan is not allowed with FULL JOIN: UNNEST expression

如何根据此架构获得预期的输出?

1 个答案:

答案 0 :(得分:3)

您可以在这里使用LEFT JOIN

#standardSQL
SELECT name, implies 
FROM `project.dataset.your_table`
LEFT JOIN UNNEST(implies) AS implies

您可以使用问题中的虚拟数据来检查

#standardSQL
WITH `project.dataset.your_table` AS (
SELECT 'Foo' name, [] implies UNION ALL
SELECT 'Bar', ['Foo'] UNION ALL
SELECT 'Baz', ['Foo', 'Bar']
)
SELECT name, implies 
FROM `project.dataset.your_table`
LEFT JOIN UNNEST(implies) AS implies

结果为

Row name    implies  
1   Foo     null     
2   Bar     Foo  
3   Baz     Foo  
4   Baz     Bar