如何将嵌套展平转换为标准SQL

时间:2017-07-11 17:41:50

标签: sql google-bigquery

目前正在使用谷歌的BigQuery。我的遗留查询如下:

Select * FROM FLATTEN(FLATTEN([DB.table1], record.atr), record.atr.value) 
WHERE record.atr.value IN "1"

我理解BigQuery now automatically flattens表格,所以我认为这样可以解决问题:

SELECT * FROM `DB.table1`
WHERE record.atr.value IN "1" 

但是我收到错误错误:无法访问类型为ARRAY,sv ARRAY,...>>的值的字段值在[2:17]

如何将此转换为标准SQL查询?

2 个答案:

答案 0 :(得分:2)

使用标准SQL时,BigQuery不会自动压缩数组;您提供的链接表示相反。在您的情况下,查询将是这样的:

#standardSQL
SELECT * EXCEPT(value), value
FROM `Db.table1`
CROSS JOIN UNNEST(record.atr) AS atr
CROSS JOIN UNNEST(atr.value) AS value;

如果您想为空数组获取一行,则可以改为使用LEFT JOIN

#standardSQL
SELECT * EXCEPT(value), value
FROM `Db.table1`
LEFT JOIN UNNEST(record.atr) AS atr
LEFT JOIN UNNEST(atr.value) AS value;

以下是您可以尝试运行的示例查询:

#standardSQL
WITH Input AS (
  SELECT STRUCT(ARRAY<STRUCT<x STRING, value ARRAY<INT64>>>[('foo', [1, 2]), ('bar', [3, 4, 5])] AS atr) AS record UNION ALL
  SELECT STRUCT(ARRAY<STRUCT<x STRING, value ARRAY<INT64>>>[('baz', [6, 7])])
)
SELECT * EXCEPT (value)
FROM Input
CROSS JOIN UNNEST(record.atr) AS atr
CROSS JOIN UNNEST(atr.value) AS value;

答案 1 :(得分:1)

  

如何将其转换为标准SQL查询?   SELECT * FROM`DB.table1`
  WHERE record.atr.value IN&#34; 1&#34;

以下是BigQuery Standard SQL

  
#standardSQL
SELECT *
FROM `DB.table1`
WHERE (SELECT COUNT(1) 
        FROM UNNEST(record.atr) AS atr, 
              UNNEST(atr.value) AS val 
        WHERE val IN ('1')) > 0  

您可以使用虚拟数据进行测试(部分借鉴了Elliott的例子)

#standardSQL
WITH `DB.table1` AS (
  SELECT 1 AS id, STRUCT([STRUCT<x STRING, value ARRAY<STRING>>('foo', ['1', '2']), ('bar', ['3', '4', '5'])] AS atr) AS record UNION ALL
  SELECT 2, STRUCT([STRUCT<x STRING, value ARRAY<STRING>>('baz', ['6', '7'])])
)
SELECT *
FROM `DB.table1`
WHERE (SELECT COUNT(1) 
        FROM UNNEST(record.atr) AS atr, 
              UNNEST(atr.value) AS val 
        WHERE val IN ('1')) > 0