theta加入hive

vi4fp9gy  于 2021-06-25  发布在  Hive
关注(0)|答案(1)|浏览(213)

我在sas中有一个θ连接,需要翻译成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条件中添加非equi连接(左表中所有不匹配的记录都丢失),就不会得到正确的结果。
在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

有什么建议吗?

u5i3ibmn

u5i3ibmn1#

正如你可能已经意识到的, left join 保留保留行表(employee)中的所有项,而 where 过滤掉那些项目如果 a.joindate<b.joindate or a.releasedate >= b.releasedate .
那些 on 条件在逻辑上解释为:
对于每个项目 li 从左表中,每当 ri 从右表中找到符合 on 条件,创建新项 ni 其列值是 li 以及 ri . 然后把 ni 在结果集中。这可能会复制左表中的行。
如果 li 找不到匹配项,请复制一个,然后用空值填充正确的列。将此项也放入结果集中。
所以我们可以通过以下方式来模拟这种行为:
松开 on 通过在 on 条款;
过滤掉由系统产生的多余行 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))

相关问题