sql—使用not in子句替代配置单元查询

r6l8ljro  于 2021-06-25  发布在  Hive
关注(0)|答案(1)|浏览(404)

我有以下一组配置单元表:

create table image_additions (
    customer_id STRING,
    image_key STRING,
    image_size STRING
);
create table image_removals (
   customer_id STRING,
   image_key STRING,
   image_size STRING
);
create table images_stored (
   customer_id STRING,
   image_key STRING,
   image_size STRING
);

我想从如下查询中运行insert:

insert into images_stored
select ia.customer_id, 
       ia.image_key, 
       ia.image_size 
from image_additions ia 
where ia.image_key not in 
       (select ir.image_key from image_removals ir);

这会产生笛卡尔积,而hive不允许我运行它。如何使用另一个查询来实现这一点?

zdwk9cvp

zdwk9cvp1#

使用left join+where为null;

insert into images_stored
select ia.customer_id, 
       ia.image_key, 
       ia.image_size 
from image_additions ia 
     left join image_removals ir on ia.image_key=ir.image_key 
where ir.image_key is null;

使用不存在:

insert into images_stored
select ia.customer_id, 
       ia.image_key, 
       ia.image_size 
from image_additions ia  
where not exists (select 1 from image_removals ir where ia.image_key=ir.image_key);

相关问题