2017-07-26 8 views
0

J'essaie de joindre deux tables, une avec un champ répété, en utilisant le SQL standard dans BigQuery. En utilisant SQL héritage que je suis venu avec cette requêteJoindre une table avec un champ répété en utilisant UNNEST()

héritage 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 

Le champ répété est le seat. J'ai essayé d'utiliser unnest() et en regardant le migration guide, mais je n'ai pas pu trouver une requête moi-même. Aide appréciée merci.

+0

Comment se présente les données et la réponse à cette question? –

Répondre

2

ci-dessous est pour 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 

Vous pouvez le tester avec des données factices comme ci-dessous

#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