如何使用来自另一个 table 的 bigquery 中的嵌套数据创建新的 table?
How to create a new table with nested data in big query from another tables?
我在 google bigquery 手册中找到了很多创建嵌套数据的示例,但是在另一个 table 中没有这样的示例。
我想使用两个现有的 table 创建一个带有嵌套数据的新 table(例如 solar_system_moons_nested)(编写 SQL 语句来生成嵌套数据) s(例如行星和卫星 tables)。我希望新的 table 如下所示:
我创建月球和行星 tables 如下:
- 月亮table
- 行星table:
是否可以从现有的 table 中创建一个嵌套的 table?任何帮助将不胜感激。
这是我制作新的 table(如下所示)的方法:
WITH solar_system_moons_nested AS (
SELECT p.planet,
STRUCT(moon ,Distance_from_Planet__km_,Diameter__km_) AS moons,
from test.planets p inner join test.moons m on m.planet=p.planet
)
select * from solar_system_moons_nested
这是它的样子:
如您所见,select 没有达到我的预期。
使用array_agg构造数组:
WITH solar_system_moons_nested AS (
SELECT
p.planet,
array_agg(STRUCT(moon ,Distance_from_Planet__km_,Diameter__km_)) AS moons,
from test.planets p inner join test.moons m on m.planet=p.planet
group by p.planet
)
select * from solar_system_moons_nested
如果您只需要一个嵌套结构,您可以使用 array_agg 并执行如下操作
WITH solar_system_moons_nested AS (
SELECT p.planet,
ARRAY_AGG(STRUCT(moon ,Distance_from_Planet__km_,Diameter__km_)) AS moons,
from test.planets p inner join test.moons m on m.planet=p.planet
GROUP BY 1
)
select * from solar_system_moons_nested
有关 array_agg 的更多信息,请点击此处 https://cloud.google.com/bigquery/docs/reference/standard-sql/aggregate_functions
我在 google bigquery 手册中找到了很多创建嵌套数据的示例,但是在另一个 table 中没有这样的示例。
我想使用两个现有的 table 创建一个带有嵌套数据的新 table(例如 solar_system_moons_nested)(编写 SQL 语句来生成嵌套数据) s(例如行星和卫星 tables)。我希望新的 table 如下所示:
我创建月球和行星 tables 如下:
- 月亮table
- 行星table:
是否可以从现有的 table 中创建一个嵌套的 table?任何帮助将不胜感激。
这是我制作新的 table(如下所示)的方法:
WITH solar_system_moons_nested AS (
SELECT p.planet,
STRUCT(moon ,Distance_from_Planet__km_,Diameter__km_) AS moons,
from test.planets p inner join test.moons m on m.planet=p.planet
)
select * from solar_system_moons_nested
这是它的样子:
如您所见,select 没有达到我的预期。
使用array_agg构造数组:
WITH solar_system_moons_nested AS (
SELECT
p.planet,
array_agg(STRUCT(moon ,Distance_from_Planet__km_,Diameter__km_)) AS moons,
from test.planets p inner join test.moons m on m.planet=p.planet
group by p.planet
)
select * from solar_system_moons_nested
如果您只需要一个嵌套结构,您可以使用 array_agg 并执行如下操作
WITH solar_system_moons_nested AS (
SELECT p.planet,
ARRAY_AGG(STRUCT(moon ,Distance_from_Planet__km_,Diameter__km_)) AS moons,
from test.planets p inner join test.moons m on m.planet=p.planet
GROUP BY 1
)
select * from solar_system_moons_nested
有关 array_agg 的更多信息,请点击此处 https://cloud.google.com/bigquery/docs/reference/standard-sql/aggregate_functions