使用 UNNEST() 将 table 与重复字段连接起来
Join a table with a repeated field using UNNEST()
我正在尝试使用 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,但我自己无法提出查询。帮助感谢感谢。
以下适用于 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
我正在尝试使用 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,但我自己无法提出查询。帮助感谢感谢。
以下适用于 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