簡體   English   中英

Hive 加入分區

[英]Hive join over partition

我有這兩張桌子:

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_id”但超過表place_of_sale 的分區“國家”的2 個表?

這是一個具有所需結果的示例:

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

這里的示例僅給出了 2 個不同的國家,但可以想象有很多不同的國家。 這就像為每個國家/地區執行左連接,然后在所有國家/地區的結果之間進行聯合。

如果售出的產品是表 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

暫無
暫無

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

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