BigQuery Standard SQL“左”交叉连接嵌套数组

时间:2017-09-14 10:43:17

标签: google-bigquery

我正在尝试对包含嵌套数组的记录的表运行查询。 在下面的示例中,嵌套数组称为“年龄”。一些记录可能有一个空数组。

如何运行查询运行“左”交叉连接,以便即使输出没有年龄元素的记录(一行有年龄= null)/

 WITH people AS (
 select * from unnest(
 [STRUCT("Tim" as name, [5] as ages),
  STRUCT("Jane" as name, [] as ages)])
)

select 
  name
  from people, unnest(ages) as a

2 个答案:

答案 0 :(得分:2)

#standardSQL
WITH people AS (
 SELECT * FROM UNNEST(
 [STRUCT("Tim" AS name, [5] AS ages),
  STRUCT("Jane" AS name, [] AS ages)])
)

SELECT 
  name
FROM people
LEFT JOIN UNNEST(ages) AS a

答案 1 :(得分:0)

我最近遇到了一些问题,想添加另一种看待它的方式:

WITH people AS (
 SELECT * FROM UNNEST(
 [STRUCT("Tim" AS name, [5] AS ages),
  STRUCT("Jane" AS name, [] AS ages)])
)

SELECT 
  name
FROM people, UNNEST(ages) AS a

相同
WITH people AS (
 SELECT * FROM UNNEST(
 [STRUCT("Tim" AS name, [5] AS ages),
  STRUCT("Jane" AS name, [] AS ages)])
)

SELECT 
  name
FROM people
CROSS JOIN UNNEST(ages) AS a

相同
WITH people AS (
 SELECT * FROM UNNEST(
 [STRUCT("Tim" AS name, [5] AS ages),
  STRUCT("Jane" AS name, [] AS ages)])
)

SELECT 
  name
FROM people
LEFT JOIN UNNEST(ages) AS a
WHERE a IS NOT NULL