Hive之——怎样写exist/in以及not exists/not in子句

Hive 不支持 where 子句中的子查询, SQL 常用的 exist in 子句需要改写。这一改写相对简单。考虑以下 SQL 查询语句:

SELECT a.key, a.value
FROM a
WHERE a.key in
(SELECT b.key
FROM B);

可以改写为

SELECT a.key, a.value
FROM a LEFT OUTER JOIN b ON (a.key = b.key)
WHERE b.key <> NULL;

一个更高效的实现是利用 left semi join 改写为:

SELECT a.key, a.val
FROM a LEFT SEMI JOIN b on (a.key = b.key);

left semi join 是 0.5.0 以上版本的特性。hive 的 left semi join 讲解https://blog.csdn.net/happyrocking/article/details/79885071

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

not exists 例子


select a, b
  from table1 t1
 where not exists (select 1
          from table2 t2
         where t1.a = t2.a
           and t1.b = t2.b)

可以改为



select t1.a, t2.b
  from table1 t1
  left join table2 t2
    on (t1.a = t2.a and t1.b = t2.b)
 where t2.a is null

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值