Theta join in Hive

问题描述 投票:1回答:1

我在SAS中有theta联接,需要转换为Hive。

SAS:

select a.id,b.name from employee a 
left outer join company b 
on ( a.id=b.id and a.joindate>=b.joindate and a.releasedate < b.releasedate)

由于这不是内部联接,如果我在where条件中添加非等联接(缺少左表中的所有不匹配记录),则不会得到正确的结果。

下面在Hive中尝试过:

select a.id,b.name from employee a 
left outer join company b 
on ( a.id=b.id) 
where a.joindate>=b.joindate and a.releasedate < b.releasedate

有什么建议吗?

hive hiveql hadoop2
1个回答
2
投票

正如您可能已经意识到的那样,left join保留保留行表(员工)中的所有项目,而如果where,则a.joindate<b.joindate or a.releasedate >= b.releasedate会过滤掉这些项目。

这些on条件被逻辑上>>解释为:

  1. 对于左表中的每个项目li,只要发现右表中的项目ri都满足on条件,请创建一个新项目ni,其列值为li的组合和ri。然后将ni放入结果集中。这可能会复制左侧表格中的行。
  2. 如果li找不到匹配项,请对其进行一份复制,并在右列中填充空值。将此项目也放入结果集。

因此我们可以通过以下方式模仿此行为:

  1. 通过在on子句中仅保留相等条件来松开on条件;
  2. 过滤掉因宽松的on条件而导致的多余行,即,不满足除相等条件以外的条件的行。

结果查询可能看起来像这样:

select 
    a.id,
    b.name
from employee a 
left outer join company b 
on (a.id=b.id)
where (b.id is null 
       or (a.joindate>=b.joindate and a.releasedate<b.releasedate))
© www.soinside.com 2019 - 2024. All rights reserved.