簡體   English   中英

Hive加入!=不工作

[英]Hive Join where != not working

在蜂巢中,我正在使用兩個表,每個表都有相同的字段。 我的想法是我只想要表“一”中的行,這些行不在表“兩”中。 這時,我沒有得到任何返回的結果。

表一

id  | category
i_1 | c_123
i_1 | c_234
i_1 | c_345
i_1 | c_456
i_2 | c_456

表二

id  | category 
i_1 | c_345

這是我當前的代碼(不返回任何行):

select a.id, a.category
from one a 
left join two b 
on a.id=b.id 
where a.category != b.category;  

理想的結果應如下所示:

id  | category
i_1 | c_123
i_1 | c_234
i_1 | c_456
i_2 | c_456

也許這就是你要找的東西:

select a.id, a.category
from one a 
  left join two b 
    on a.id=b.id 
      and a.category = b.category
where b.id is null

這將返回表一中不存在的所有記錄,這兩個記錄在id和類別上匹配。 您的查詢是否定outer join 我不確定hive支持not exists ,但這甚至可能更有效。

此解決方案僅適用於Hive 0.13+

這是 NOT EXISTS子句的手冊參考。

select a.id, a.category
from one a 
where not exists (
  select 1 
  from two b 
  where a.id = b.id and a.category = b.category
)

值得一提的是,它可以比某些DBMS上的WHERE x IS NULL LEFT JOIN執行得更快。

我認為這是一種更好的做法,因為您不需要讀取整個查詢以注意LEFT JOIN實際上是反連接。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM