【问题标题】:Join a table with a repeated field using UNNEST()使用 UNNEST() 连接具有重复字段的表
【发布时间】:2017-07-26 21:20:15
【问题描述】:

我正在尝试使用 BigQuery 中的标准 SQL 连接两个表,其中一个具有重复的字段。使用 Legacy SQL 我想出了这个查询

旧版 SQL

SELECT
  b.*,
  t.field1,
  t.field2
FROM
  FLATTEN([table1],repeated_field) AS b
LEFT JOIN
  [table2] AS t
ON
  b.Row = t.RowLabel
  b.seat = t.SeatLabel

重复的字段是seat。我尝试使用unnest() 并查看migration guide,但自己无法提出查询。帮助感谢感谢。

【问题讨论】:

  • 这个查询的数据和响应看起来如何?

标签: sql join google-bigquery unnest bigquery-standard-sql


【解决方案1】:

以下是 BigQuery 标准 SQL

#standardSQL
SELECT   
  b.*,
  t.field1,
  t.field2
FROM `table1` AS b, UNNEST(Seats) AS Seat
JOIN `table2` AS t
ON b.Row = t.RowLabel
AND Seat = t.SeatLabel  

您可以使用如下的虚拟数据对其进行测试

#standardSQL
WITH `table1` AS (
  SELECT '1' AS Row, ['a', 'b', 'c'] AS Seats
),
`table2` AS (
  SELECT '1' AS RowLabel, 'b' AS SeatLabel, 111 AS field1, 222 AS field2 UNION ALL
  SELECT '1' AS RowLabel, 'a' AS SeatLabel, 111 AS field1, 222 AS field2 UNION ALL
  SELECT '1' AS RowLabel, 'd' AS SeatLabel, 111 AS field1, 222 AS field2
)
SELECT   
  b.*,
  t.field1,
  t.field2
FROM `table1` AS b, UNNEST(Seats) AS Seat
JOIN `table2` AS t
ON b.Row = t.RowLabel
AND Seat = t.SeatLabel

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-11
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    • 2016-01-06
    • 2018-03-16
    • 1970-01-01
    • 2012-06-05
    相关资源
    最近更新 更多