hive 查询从最新的分区中选择行

fruv7luv  于 2021-04-09  发布在  Hive
关注(0)|答案(1)|浏览(475)

我在hive有一个分区表,模式和示例如下所示

item_id | price | brand | partition_id
AX_12      340.22  Apple.    356
AZ_47      230.00  Samsung   357
AX_12      321.00. Apple.    357
AQ_17.     125.00  Lenovo.   356

如果一个项目存在于多个分区中,我需要选择最新的分区的行,所以这个例子的预期输出是这样的

item_id | price | brand | partition_id
AX_12      321.00  Apple.    357
AZ_47      230.00  Samsung   357
AQ_17.     125.00  Lenovo.   356

表中有10个分区,每个分区有1000万行。

hujrc8aj

hujrc8aj1#

您可以使用窗口功能对每组的顶部记录进行过滤。

select t.*
from (
    select t.*, row_number() over(partition by item_id order by partition_id desc) rn 
    from mytable t
)
where rn = 1

一个典型的替代方法是用一个相关的子查询来过滤。

select t.*
from mytable t
where t.partition_id = (
    select max(t1.partition_id) from mytbale t1 where t1.item_id = t.item_id
)

相关问题