HossBender
HossBender

Reputation: 1079

UNNEST and JOIN | Big Query

I'm able to unnest one table in Big Query by using the following code:

SELECT * EXCEPT(instance, line_items) FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY id) AS instance
  FROM `shopify.orders`
), UNNEST(line_items) as item

WHERE instance = 1

Additionally, I'm able to JOIN this table (unnested) with another table using the following code:

SELECT * FROM (
  SELECT *, ROW_NUMBER() OVER(PARTITION BY id) AS instance
  FROM `shopify.orders`
  JOIN `google_analytics.GA`
  ON name = TransactionID

)

WHERE instance = 1

However, I'm not sure how to JOIN these two tables while still unnesting the values from my first query. Any idea of how I can join an UNNESTED table with another table?

Upvotes: 0

Views: 2790

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269553

Doesn't join work?

SELECT * EXCEPT(instance, line_items)
FROM (SELECT *, ROW_NUMBER() OVER(PARTITION BY id) AS instance
      FROM `shopify.orders`
     ) o CROSS JOIN
     UNNEST(o.line_items) as item JOIN
     `google_analytics.GA` ga
      ON name = TransactionID

Upvotes: 1

Related Questions