1

我正在尝试使用 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()并查看迁移指南,但自己无法提出查询。帮助感谢感谢。

4

1 回答 1

3

以下是 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
于 2017-07-26T22:06:27.100 回答