在Hive中使用join sql查找每个类别的最低价格 [英] Finding the lowest price for each category using join sql in Hive
本文介绍了在Hive中使用join sql查找每个类别的最低价格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须编写Hive SQL。
我想为每个类别找到最低价格。我想为每个类别获得一行,包括类别ID,产品ID,价格(最低价格)和图片url列。
在下面的例子中,我们最后需要两行。
帮助我。
$ b
category_product_match表
category_id product_id
1001 500001
1001 500002
1002 500101
1002 500102
1002 500103
product_info表
prd_id价格img_url
500001 29000 / app / url / img /500001.jpg
500002 29500 /app/url/img/500002.jpg
500101 8100 /app/url/img/500101.jpg
500102 8100 / app / url / img / 500102 .jpg
500103 8500 /app/url/img/500103.jpg
解决方案您可以使用窗口函数:
select pc。*
from(select pi。 *,cpm.category_id,
row_number()over(由cpm.category_id order by pi.price asc分区)as_seqnum
from product_info pi join
category_product_match cp m
on cpm.product_id = pi.prd_id
)pc
其中seqnum = 1;
I have to write Hive SQL.
I want to find the lowest price for each category. I want to get one row for each category with the category ID, the product ID, the price (lowest price), and the image url columns. In the example below, we finally need two rows.
Help me. Thank you.
"category_product_match" table
category_id product_id
1001 500001
1001 500002
1002 500101
1002 500102
1002 500103
"product_info" table
prd_id price img_url
500001 29000 /app/url/img/500001.jpg
500002 29500 /app/url/img/500002.jpg
500101 8100 /app/url/img/500101.jpg
500102 8100 /app/url/img/500102.jpg
500103 8500 /app/url/img/500103.jpg
解决方案
You can use window functions:
select pc.*
from (select pi.*, cpm.category_id,
row_number() over (partition by cpm.category_id order by pi.price asc) as seqnum
from product_info pi join
category_product_match cpm
on cpm.product_id = pi.prd_id
) pc
where seqnum = 1;
这篇关于在Hive中使用join sql查找每个类别的最低价格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文