6

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`
4
  • What is item_details? I don't see it in the sample data for the Orders table. Commented Aug 10, 2016 at 15:37
  • 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. Commented Aug 10, 2016 at 18:37
  • what is expected output? - still not clear - is it order's fields plus total price for all items in order? Commented Aug 11, 2016 at 0:48
  • I need to replace item_ids array with array of prices Commented Aug 11, 2016 at 0:49

2 Answers 2

10

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

Sign up to request clarification or add additional context in comments.

2 Comments

Well my array creation expression is actually same, I guess I get error, because MenuItems is an another table.
Hope my "excercise" helped anyway!
8

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.