首页 > 解决方案 > 从mysql查询在hive中实现不等式连接

问题描述

我正在尝试在 hive 中实现一个用 mysql 编写的查询。我知道 hive 不支持 ON 条件下的不等式连接。下面是我的代码,并告诉我实现它的方法。

Select test1.a,
test2.b,
test4.c,
dummy.c
from
test1 join test2 on test1.id = test2.id and test2 != 'ABC'
join test3 on test1.id = test2.id and test3 != 'Archive'
join test4 on test3.id = test4.id and test4 = 'XYZ'
left outer join
(select test1.a,
test2,b
test3.c
from test1 join test2 on test1.id = test2.id and test2 != 'ABC'
join test3 on test1.id = test2.id) dummy
on test3.id = dummy.id
**and (test4.id != 1001 or dummy.c = TRUE)**
left join test5 on test3.id= test5.id
and dummy.c = TRUE

现在用 * 突出显示的条件是我需要知道如何在 hive 中实现它的部分,因为我无法在 ON 条件下实现它,并且如果我将它放在 where 子句结果不匹配。任何在 hive 中重写它的建议将不胜感激。

标签: mysqlhivehiveqlinequality

解决方案


对于从 LEFT JOIN 中选择的列,我在 SELECT 语句中使用了不等式条件作为 case 语句。下面是代码 -

Select test1.a,
test2.b,
test4.c,
case when (test4.id != 1001 or nvl(dummy.c , False))= TRUE then dummy.c end as c0
from
test1 join test2 on test1.id = test2.id and test2 != 'ABC'
join test3 on test1.id = test2.id and test3 != 'Archive'
join test4 on test3.id = test4.id and test4 = 'XYZ'
left outer join
(select test1.a,
test2,b
test3.c
from test1 join test2 on test1.id = test2.id and test2 != 'ABC'
join test3 on test1.id = test2.id) dummy
on test3.id = dummy.id
left join test5 on test3.id= test5.id
and dummy.c = TRUE

推荐阅读