Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hive doesn't support in, exists. How do I write the following query?

I have two tables A and B that both have a column id. I wish to obtain ids from A that are not present in B. The obvious way is:

SELECT id FROM A WHERE id NOT IN (SELECT id FROM B)

Unfortunately, Hive doesn't support in, exists or subqueries. Is there a way to achieve the above using joins?

I thought of the following

SELECT A.id FROM A,B WHERE A.id<>B.id

But it seems like this will return the entirety of A, since there always exists an id in B that is not equal to any id in A.

like image 286
elexhobby Avatar asked May 29 '13 02:05

elexhobby


2 Answers

You can do the same with a LEFT OUTER JOIN in Hive:

SELECT A.id
FROM A
LEFT OUTER JOIN B
ON (B.id = A.id)
WHERE B.id IS null
like image 55
Charles Menguy Avatar answered Oct 21 '22 06:10

Charles Menguy


Hive seems to support IN, NOT IN, EXIST and NOT EXISTS from 0.13.

select count(*)
from flight a
where not exists(select b.tailnum from plane b where b.tailnum = a.tailnum);

The subqueries in EXIST and NOT EXISTS should have correlated predicates (like b.tailnum = a.tailnum in above sample) For more, refer Hive Wiki > Subqueries in the WHERE Clause

like image 39
Sangmoon Oh Avatar answered Oct 21 '22 07:10

Sangmoon Oh