如何替换HIVE Join中的OR条件

时间:2016-12-22 06:06:46

标签: hive hiveql impala bigdata

我有以下2个表格

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

我想对这些表执行以下查询,但遗憾的是HIVE在JOIN条件下不支持OR。如何在没有OR条件的情况下重写查询,从而得到相同的结果

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 ))  

3 个答案:

答案 0 :(得分:1)

您可以将查询重新编写为具有union all的两个选择,如下所示:

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 ='';

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

答案 1 :(得分:1)

在hive中使用半left join

来解决多个equerry问题
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 )

答案 2 :(得分:0)

只需在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 )