簡體   English   中英

使用UNNEST()加入包含重復字段的表

[英]Join a table with a repeated field using UNNEST()

我正在嘗試使用BigQuery中的標准SQL連接兩個表,一個帶有重復字段。 使用傳統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()並查看遷移指南 ,但我自己無法提出查詢。 感謝謝謝。

下面是BigQuery Standard 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM