2
votes

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`
2
What is item_details? I don't see it in the sample data for the Orders table. - Elliott Brossard
i would guess that item_details is the record in one of the table. Still, please provide schema of your tables with few rows example and expected result. - Generically speaking - please edit your question to show a Minimal, Complete, and Verifiable example of the code that you are having problems with, then we can try to help with the specific problem. You can also read How to Ask. - Mikhail Berlyant
what is expected output? - still not clear - is it order's fields plus total price for all items in order? - Mikhail Berlyant
I need to replace item_ids array with array of prices - potykion

2 Answers

4
votes

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

3
votes

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