如何替换hive join中的or条件

wlwcrazw  于 2021-06-28  发布在  Hive
关注(0)|答案(3)|浏览(929)

我有下面两张table

Employee(age,name,dpt_cd,dpt_rg_cd)
Department(dpt_id,dpt_cd,dpt_rg_cd)

我想对这些表执行以下查询,但不幸的是,配置单元不支持或处于连接条件。如果没有或没有给出相同结果的条件,如何重写查询

SELECT * FROM employee LEFT OUTER JOIN department ON (employee.dpt_cd =department.dpt_cd OR (employee.dpt_cd ='' AND employee.dpt_rg_cd= employee.dpt_rg_cd ))
nwsw7zdq

nwsw7zdq1#

只需在where子句中使用条件,并在on子句中输入1=1。如下所示:

SELECT * FROM employee LEFT OUTER JOIN department ON ( 1=1) 
where employee.dpt_cd =department.dpt_cd OR 
      (employee.dpt_cd ='' AND employee.dpt_rg_cd= employee.dpt_rg_cd )
ars1skjm

ars1skjm2#

您可以将查询重写为两个具有联合的select,如下所示:

select * from employee left outer join department on (employee.dpt_cd =department.dpt_cd)
union all
select * from employee left outer join department on (employee.dpt_rg_cd = employee.dpt_rg_cd) where employee.dpt_cd ='';

这可能是一个非常慢的查询,但应该会产生您想要的结果。

nafvub8i

nafvub8i3#

要解决配置单元中的多个等式问题,请使用半左连接ie

select x.*
from employee x
LEFT SEMI JOIN  department   b on (x.buyer_id= b.id )
LEFT SEMI JOIN  department   c on (x.seller_id= c.id )

相关问题