分区上的配置单元连接

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

我有两张table:

table products
(
product_id bigint, 
product_name string
)
partitioned by (product_category as string)

table place_of_sale
(
product_id bigint, 
city string
)
partitioned by (country as string)

如何基于'product\u id'在表place\u of seal的分区'country'上左键联接这两个表?
这是一个具有预期结果的示例:

table products
product_id      product_name    product_category
1000            banana          fruit
1001            coconut         fruit
1002            ananas          fruit
2002            cow             animal
2003            beef            animal

table place_of_sale
product_id      city        country
1000            Texas       USA
1002            Miami       USA
2003            Sydney      Australia

desired result for a left join between table products and table place_of_sale over the partition country :

product_id      product_name        product_category    city   country
1000            banana              fruit               Texas  USA
1001            coconut             fruit               null   null
1002            ananas              fruit               Miam   USA

2002            cow                 animal              null   null
2003            beef                animal              Sydney Australia

这里的例子只给出了两个不同的国家,但想象很多不同的国家。这就像是为每个国家执行的左连接,然后是所有国家结果之间的联合。

xdnvmnnf

xdnvmnnf1#

如果销售的产品是表place of sale中的产品,则使用left join:

select s.country, p.product_id, p.product_name, p.category, 
       case when s.product_id is NULL then "not sold" else "sold" as sold 
 from products p
      left join place_of_sale s on p.product_id = s.product_id 
 order by country, product_id --order if necessary

相关问题