RogerFromSpace
RogerFromSpace

Reputation: 446

clickhouse : left join using IN

I wish to perform a left join based on two conditions :

SELECT
  ...
FROM
   sometable AS a
LEFT JOIN someothertable AS b ON a.some_id = b.some_id 
  AND b.other_id IN (1, 2, 3, 4)

I got the error:

Supported syntax: JOIN ON Expr([table.]column, ...) = Expr([table.]column, ...) [AND Expr([table.]column, ...) = Expr([table.]column, ...) ...]```

It seems that the condition for a join must be = and can't be in

Any idea ?

Upvotes: 0

Views: 1935

Answers (1)

vladimir
vladimir

Reputation: 15218

Consider moving IN-operator into subquery:

SELECT
    a.number,
    b.number
FROM numbers(8) AS a
LEFT JOIN 
(
    SELECT *
    FROM numbers(234)
    WHERE number IN (1, 2, 3, 4)
) AS b USING (number)

/*
┌─number─┬─b.number─┐
│      0 │        0 │
│      1 │        1 │
│      2 │        2 │
│      3 │        3 │
│      4 │        4 │
│      5 │        0 │
│      6 │        0 │
│      7 │        0 │
└────────┴──────────┘
*/

or

SELECT
    a.number,
    b.number
FROM numbers(8) AS a
LEFT JOIN 
(
    SELECT *
    FROM numbers(234)
    WHERE number IN (1, 2, 3, 4)
) AS b USING (number)
SETTINGS join_use_nulls = 1 /* 1 is 'JOIN behaves the same way as in standard SQL. The type of the corresponding field is converted to Nullable, and empty cells are filled with NULL.' */

/*
┌─number─┬─b.number─┐
│      0 │     ᴺᵁᴸᴸ │
│      1 │        1 │
│      2 │        2 │
│      3 │        3 │
│      4 │        4 │
│      5 │     ᴺᵁᴸᴸ │
│      6 │     ᴺᵁᴸᴸ │
│      7 │     ᴺᵁᴸᴸ │
└────────┴──────────┘
*/

Upvotes: 1

Related Questions