I'm looking for some help understanding this error I'm getting in BigQuery:
LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.
I'm trying to use a case statement to alter the rows selected for joining depending on a value in the left table's row. I am doing something similar in some other places and it works, so part of me thinks I may be making a mistake regarding table alias and column names, but I can't figure it out. Here is a minimal example of what I'm trying to do:
WITH t1 AS (
SELECT "milk" AS dairy,
1 AS id,
2 AS other_id
UNION ALL
SELECT "yogurt" AS dairy,
3 AS id,
4 AS other_id
UNION ALL
SELECT "cheese" AS dairy,
5 AS id,
6 AS other_id
),
t2 AS (
SELECT "blue" AS color,
1 AS id
UNION ALL
SELECT "red" AS color,
4 AS id
)
SELECT
t1.*, t2
FROM t1
LEFT JOIN t2 ON
CASE
WHEN t1.dairy = 'milk' THEN t1.id = t2.id
WHEN t1.dairy = 'yogurt' THEN t1.other_id = t2.id
END
The result I would like to see is:
As you can see in the desired result, when the value for dairy
is milk
, I want the id
from t2
to equal the id
column in t1
, but when the value for dairy
is yogurt
, I want the id
from t2
to equal the other_id
column in t1
.
I've been searching around for an explanation but can't figure it out. I also tried the solution offered here, but got the same error, which is why I think I am simply messing something up with tables names or aliases.
Please help!
UPDATE
I was able to get rid of the error by rewriting the case statement this way:
SELECT
t1.*, t2
FROM t1
LEFT JOIN t2 ON
CASE
WHEN t1.dairy = 'milk' THEN t1.id
WHEN t1.dairy = 'yogurt' THEN t1.other_id
END = t2.id
However, in my real problem I need to join a third table in a similar fashion. If t2.color
is blue
, I want to join based on t2.id = t3.id
, but if t2.color
is red
I want to join based on t2.id = t3.other_id
. As soon as I do that, the same error occurs. Here is the full example of my attempt:
WITH t1 AS (
SELECT "milk" AS dairy,
1 AS id,
2 AS other_id
UNION ALL
SELECT "yogurt" AS dairy,
3 AS id,
4 AS other_id
UNION ALL
SELECT "cheese" AS dairy,
5 AS id,
6 AS other_id
),
t2 AS (
SELECT "blue" AS color,
1 AS id
UNION ALL
SELECT "red" AS color,
4 AS id
),
t3 AS (
SELECT "sunny" AS weather,
1 AS id,
10 AS other_id
UNION ALL
SELECT "cloudy" AS weather,
11 AS id,
4 AS other_id
)
SELECT
t1.*, t2, t3
FROM t1
LEFT JOIN t2 ON
CASE
WHEN t1.dairy = 'milk' THEN t1.id
WHEN t1.dairy = 'yogurt' THEN t1.other_id
END = t2.id
LEFT JOIN t3 ON
CASE
WHEN t2.color = 'blue' THEN t3.id
WHEN t2.color = 'red' THEN t3.other_id
END = t2.id
But now the same error occurs:
LEFT OUTER JOIN cannot be used without a condition that is an equality of fields from both sides of the join.
If I remove the joining of t3
, it works correctly. Here are some more images of the tables and desired result in case it helps:
case
statement in yourselect
statement. – rtenhacase
statement doesn't define how much of t2 will be scanned. – rtenha