Hive加入重复键

时间:2015-09-02 14:33:49

标签: sql join hive

我有两张表,比如

表1:

c_id,   p_id,   sales  
x,   1, 1  
x,  2,  1  
x,  3,  1  
x,  4,  1  
y,  5,  1  
y,  4,  1  
y,  3,  1  
y,  6,  1  
y,  7,  1  
z,  1,  1  
z,  8,  1  

表2:

c_id,   p_id,   sales  
x,  8,  1  
x,  2,  1  
x,  7,  1  
y,  5,  1  
y,  2,  1  
y,  3,  1  
z,  3,  1  

我想得到表2中但不存在于table1中的c_id和p_id对。所以结果应该是这样的:

c_id    p_id    sales  
x,  8,  1  
x,  7,  1  
y,  2,  1  
z,  3,  1  

我使用了以下代码,但它不起作用:

select b.c_id, b.p_id, b.sales  
from table1 as a  
join table2 as b  
on a.c_id = b.c_id
where a.p_id != b.p_id

感谢

1 个答案:

答案 0 :(得分:0)

您想使用left join,然后检查不匹配:

select b.c_id, b.p_id, b.sales
from table2 b left join
     table1 a
     on a.c_id = b.c_id and a.p_id = b.p_id
where a.c_id is null;