使用 Flatten to select where var1 (non-repeated) = "abc" from a bigquery table which contains multiple nested variables?
Using Flatten to select where var1 (non-repeated) = "abc" from a bigquery table which contains multiple nested variables?
我是 BigQuery 的新手(使用它的第 3 天,没有经过培训),我只是想了解嵌套字段等。
我查看了以下资源并使用了 google bigquery 文档中的 personsdata 示例 link
https://cloud.google.com/bigquery/docs/data
我想运行以下查询:
select *
from [dataset.tableid]
where fullname = 'John Doe'
如果我运行这个,我得到以下错误:
错误:无法同时输出多个独立重复的字段。找到 children_age 和 citiesLived_place
从阅读上面的文章来看,这是不可能的,因为你需要将结果展平,据我所知,这只是重复了所有 none 重复的变量,即
全名 |年龄 |性别 | Children.name | children.age
李四 | 22 |男 |约翰 | 5
李四 | 22 |男 |简 | 7
上面的一篇文章建议您仍然可以通过使用 bigquery 中的 flatten 函数来使用 where 语句:
select fullname,
age,
gender,
citiesLived.place
FROM (FLATTEN([dataset.tableId], children))
WHERE
(citiesLived.yearLived > 1995) AND
(children.age > 3)
GROUP BY fullName, age, gender, citiesLived.place
如果我将其更改为:
select *
FROM (FLATTEN([dataset.tableId], children))
WHERE fullname = 'John Doe'
那么这很好用并且给了我我需要的东西但是如果我改成这个:
select *
FROM (FLATTEN([dataset.tableId], citieslived))
WHERE fullname = 'John Doe'
然后我得到以下错误:
错误:无法同时输出多个独立重复的字段。找到 children_age 和 citiesLived_yearsLived
有人可以解释为什么这会基于 "Children" 而不是 "CitiesLived" 进行展平,以及如何知道在具有多个嵌套变量的更复杂数据集的展平中使用哪些变量?
提前致谢
Can someone explain why this will work flattening based on "Children" but not "CitiesLived"
再次检查此 table 的架构
Schema
-----------------------------------
|- kind: STRING
|- fullName: STRING (required)
|- age: INTEGER
|- gender: STRING
+- phoneNumber: RECORD
| |- areaCode: INTEGER
| |- number: INTEGER
+- children: RECORD (repeated)
| |- name: STRING
| |- gender: STRING
| |- age: INTEGER
+- citiesLived: RECORD (repeated)
| |- place: STRING
| +- yearsLived: INTEGER (repeated)
如您所见 - 当您展平 children
重复记录时 - 唯一留给输出的重复记录是 citiesLived
并且即使它内部还有另一个重复字段 - yearsLived
– 它们 不独立 – 因此 BigQuery Legacy SQL 可以输出结果
现在,当您按 citiesLived
展平时 – 您得到的结果是两个重复的字段 - children
和 yearsLived
。这两个是 independent - 因此 BigQuery Legacy SQL 无法输出这样的结果。
how to know what variables to use within flatten with more complex datasets with multiple nested variables?
要使其工作 - 您应该添加另一个展平(例如)yearsLived
归档。类似下面
FROM (FLATTEN(FLATTEN([dataset.tableId], citieslived), yearsLived))
添加所有这些多个 FLATTEN 会变得很麻烦,因此使用 BigQuery Standard SQL 才是真正的方法!
如果你运行这个查询:
SELECT
*
FROM
(FLATTEN((FLATTEN(([project_id:dataset_id.table]), citiesLived.yearsLived)), citiesLived))
它会按预期变平。
使用 Legacy SQL 时,BQ 会尝试 flatten automatically 为您提供结果。
不过我注意到的是,如果您尝试展平其中包含其他重复字段的重复字段,那么有时您可能 运行 会出现这些错误(请注意字段 citiesLived
和 citiesLived.yearsLived
都重复了)。
所以解决这个问题的一种方法是强制对您要使用的所有重复字段进行展平操作(在我向您展示的示例中,我首先展平了 yearsLived 然后citiesLived) 并且不依赖 Legacy SQL 提供的自动展平操作。
但我强烈建议并鼓励您按照 Elliot 在他的评论中建议的那样学习 BQ 的 标准 SQL 版本。一开始它可能有一个更陡峭的学习曲线,但它会在长期 运行 中完全得到回报(并且您不会像我们必须做的那样最终不得不将所有遗留查询迁移到标准的风险公司)
我是 BigQuery 的新手(使用它的第 3 天,没有经过培训),我只是想了解嵌套字段等。
我查看了以下资源并使用了 google bigquery 文档中的 personsdata 示例 link
https://cloud.google.com/bigquery/docs/data
我想运行以下查询:
select *
from [dataset.tableid]
where fullname = 'John Doe'
如果我运行这个,我得到以下错误:
错误:无法同时输出多个独立重复的字段。找到 children_age 和 citiesLived_place
从阅读上面的文章来看,这是不可能的,因为你需要将结果展平,据我所知,这只是重复了所有 none 重复的变量,即
全名 |年龄 |性别 | Children.name | children.age
李四 | 22 |男 |约翰 | 5
李四 | 22 |男 |简 | 7
上面的一篇文章建议您仍然可以通过使用 bigquery 中的 flatten 函数来使用 where 语句:
select fullname,
age,
gender,
citiesLived.place
FROM (FLATTEN([dataset.tableId], children))
WHERE
(citiesLived.yearLived > 1995) AND
(children.age > 3)
GROUP BY fullName, age, gender, citiesLived.place
如果我将其更改为:
select *
FROM (FLATTEN([dataset.tableId], children))
WHERE fullname = 'John Doe'
那么这很好用并且给了我我需要的东西但是如果我改成这个:
select *
FROM (FLATTEN([dataset.tableId], citieslived))
WHERE fullname = 'John Doe'
然后我得到以下错误:
错误:无法同时输出多个独立重复的字段。找到 children_age 和 citiesLived_yearsLived
有人可以解释为什么这会基于 "Children" 而不是 "CitiesLived" 进行展平,以及如何知道在具有多个嵌套变量的更复杂数据集的展平中使用哪些变量?
提前致谢
Can someone explain why this will work flattening based on "Children" but not "CitiesLived"
再次检查此 table 的架构
Schema
-----------------------------------
|- kind: STRING
|- fullName: STRING (required)
|- age: INTEGER
|- gender: STRING
+- phoneNumber: RECORD
| |- areaCode: INTEGER
| |- number: INTEGER
+- children: RECORD (repeated)
| |- name: STRING
| |- gender: STRING
| |- age: INTEGER
+- citiesLived: RECORD (repeated)
| |- place: STRING
| +- yearsLived: INTEGER (repeated)
如您所见 - 当您展平 children
重复记录时 - 唯一留给输出的重复记录是 citiesLived
并且即使它内部还有另一个重复字段 - yearsLived
– 它们 不独立 – 因此 BigQuery Legacy SQL 可以输出结果
现在,当您按 citiesLived
展平时 – 您得到的结果是两个重复的字段 - children
和 yearsLived
。这两个是 independent - 因此 BigQuery Legacy SQL 无法输出这样的结果。
how to know what variables to use within flatten with more complex datasets with multiple nested variables?
要使其工作 - 您应该添加另一个展平(例如)yearsLived
归档。类似下面
FROM (FLATTEN(FLATTEN([dataset.tableId], citieslived), yearsLived))
添加所有这些多个 FLATTEN 会变得很麻烦,因此使用 BigQuery Standard SQL 才是真正的方法!
如果你运行这个查询:
SELECT
*
FROM
(FLATTEN((FLATTEN(([project_id:dataset_id.table]), citiesLived.yearsLived)), citiesLived))
它会按预期变平。
使用 Legacy SQL 时,BQ 会尝试 flatten automatically 为您提供结果。
不过我注意到的是,如果您尝试展平其中包含其他重复字段的重复字段,那么有时您可能 运行 会出现这些错误(请注意字段 citiesLived
和 citiesLived.yearsLived
都重复了)。
所以解决这个问题的一种方法是强制对您要使用的所有重复字段进行展平操作(在我向您展示的示例中,我首先展平了 yearsLived 然后citiesLived) 并且不依赖 Legacy SQL 提供的自动展平操作。
但我强烈建议并鼓励您按照 Elliot 在他的评论中建议的那样学习 BQ 的 标准 SQL 版本。一开始它可能有一个更陡峭的学习曲线,但它会在长期 运行 中完全得到回报(并且您不会像我们必须做的那样最终不得不将所有遗留查询迁移到标准的风险公司)