2017-07-26 2 views
0

Ich versuche, zwei Tabellen, eine mit einem wiederholten Feld, mit dem Standard-SQL in BigQuery zu verbinden. Mit Legacy-SQL kam ich mit dieser Abfrage nach obenVerknüpfen Sie eine Tabelle mit einem wiederholten Feld mit UNNEST()

Legacy-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 

Das wiederholte Feld ist die seat. Ich versuchte mit unnest() und Blick auf die migration guide, konnte aber selbst nicht mit einer Frage kommen. Hilfe geschätzt danke.

+0

Wie die Daten und die Antwort auf diese Abfrage aus? –

Antwort

2

unten ist für 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 

Sie können es mit Dummy-Daten testen, wie unten

#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 
Verwandte Themen