potykion
potykion

Reputation: 381

BigQuery Join Array in standard SQL

I am using standard SQL and I have table Order:

"Order" table

and I am trying to join it with table MenuItem

"MenuItem" table

on Order item_ids array and MenuItem __id__ integer column and get array of MenuItem prices, but I am getting an error:

Correlated subqueries that reference other tables are not supported unless they can be de-correlated, such as by transforming them into an efficient JOIN.

How to avoid this error?

Query:

WITH menu_items AS 
( 
    SELECT
        __id__,
        price
    FROM
        `potykion.MenuItem`
)
SELECT
    *, 
    ARRAY(
        SELECT 
           price
        FROM 
           UNNEST(item_ids) AS id
        JOIN 
            menu_items 
        ON 
            id = menu_items.__id__
    ) 
FROM 
    `potykion.Order`

Upvotes: 6

Views: 18011

Answers (2)

potykion
potykion

Reputation: 381

Solution with join inside array creation expression is correct, but it doesn't work with separate tables. Alternative solution is array aggregation:

WITH Orders AS (
  SELECT 1 AS id, ARRAY[1,2,3] AS item_ids UNION ALL 
  SELECT 2 AS id, ARRAY[4,5] AS item_ids UNION ALL 
  SELECT 3 AS id, ARRAY[1,4,6] AS item_ids 
),
MenuItems AS (
  SELECT 1 AS __id__, 1.1 AS price UNION ALL
  SELECT 2 AS __id__, 1.2 AS price UNION ALL
  SELECT 3 AS __id__, 1.3 AS price UNION ALL
  SELECT 4 AS __id__, 1.4 AS price UNION ALL
  SELECT 5 AS __id__, 1.5 AS price UNION ALL
  SELECT 6 AS __id__, 1.6 AS price UNION ALL
  SELECT 7 AS __id__, 1.7 AS price 
)
SELECT 
  id, ARRAY_AGG(price)
FROM Orders  
  JOIN MenuItems ON __id__ in UNNEST(item_ids)
  GROUP BY id

Upvotes: 8

Mikhail Berlyant
Mikhail Berlyant

Reputation: 172993

Try below (BigQuery Standard SQL)

WITH Orders AS (
  SELECT 1 AS id, ARRAY[1,2,3] AS item_ids UNION ALL 
  SELECT 2 AS id, ARRAY[4,5] AS item_ids UNION ALL 
  SELECT 3 AS id, ARRAY[1,4,6] AS item_ids 
),
MenuItems AS (
  SELECT 1 AS __id__, 1.1 AS price UNION ALL
  SELECT 2 AS __id__, 1.2 AS price UNION ALL
  SELECT 3 AS __id__, 1.3 AS price UNION ALL
  SELECT 4 AS __id__, 1.4 AS price UNION ALL
  SELECT 5 AS __id__, 1.5 AS price UNION ALL
  SELECT 6 AS __id__, 1.6 AS price UNION ALL
  SELECT 7 AS __id__, 1.7 AS price 
)
SELECT 
  *, 
  ARRAY(
    SELECT price 
    FROM UNNEST(item_ids) AS id 
    JOIN MenuItems 
    ON __id__ = id
  ) AS prices
FROM Orders  

Table Orders:
Orders

Table MenuItems:
Menu Items

Result:
Result

Upvotes: 10

Related Questions