hive 配置单元QL Except子句

gfttwv5a  于 2022-11-23  发布在  Hive
关注(0)|答案(3)|浏览(228)

如何在配置单元QL中执行EXCEPT子句(如SQL)
我有2个表,每个表都是一列唯一ID。
我想查找只在表1中而不在表2中的id列表

表1

apple
orange
pear

表二

apple
orange

在SQL中,您可以执行EXCEPT子句(http://en.wikipedia.org/wiki/Set_operations_%28SQL%29),但在Hive QL中无法执行此操作

x759pob2

x759pob21#

我不认为有任何内置的方式来做到这一点,但一个LEFT OUTER JOIN应该做的伎俩。
这将选择table1中 * 不 * 存在于table2中的所有Id:

SELECT t1.id FROM table1 t1 LEFT OUTER JOIN table2 t2 ON (t1.id=t2.id) WHERE t2.id IS NULL;
nhhxz33t

nhhxz33t2#

我们可以在配置单元中使用NOT EXISTS子句作为MINUS等价项。

SELECT t1.id FROM t1 WHERE NOT EXISTS (SELECT 1 from t2 WHERE t2.id = t1.id);
vtwuwzda

vtwuwzda3#

一句话:

select distinct id from table1 where id not in (select distinct id from table2)

二:

select t1.id
from table1 as t1
left join table2 as t2
on t1.id = t2.id
where t2.id is null

相关问题