使用条件之间连接 ClickHouse 中的表

问题描述

我发现 ClickHouse 中的 join 仅支持相等表达式。 但是我需要在 ClickHouse 中加入两个具有“介于”条件的大表。

如何实现这个逻辑?

select a.*,b.name
from a
join b
  on a.id = b.id
  and a.start_dt between b.start_dt and b.end_dt;

出错

代码:403,e.displayText() = DB::Exception:JOIN ON 的表达式无效。预期等于表达式...

解决方法

试试这个:

select a.*,b_name
from (
  select a.*,b.name AS b_name,b.start_dt AS b_start_dt,b.end_dt AS b_end_dt
  from a join b using id
  where a.start_dt between b_start_dt and b_end_dt
  )

查看 Clickhouse join with condition 中的一些 JOIN 细节。