查找给定客户ID的最高价格

时间:2018-06-18 13:59:44

标签: hive hiveql

我需要编写一个配置单元查询。我正在处理一个包含三列的数据集:客户ID,产品ID和价格。我需要编写一个查询,输出客户购买的最大项目的客户ID和产品ID列。

2 个答案:

答案 0 :(得分:0)

SELECT [customer], [product] FROM table WHERE [price] = (SELECT MAX(t.[price]) AS price 
FROM table as t WHERE t.[customer] = [customer])

如果您想要找到客户购买的最贵的商品,可能是这样的吗?我不确定语法是否100%正确,但它应该给你一些东西。我在下面为Hive添加了一个备忘单。

Hive Cheat Sheet

答案 1 :(得分:0)

使用row_number()

select Customer_ID, Product_ID
from
(select Customer_ID, 
        Product_ID, 
        row_number () over ( partition by Customer_ID order by Price desc) rn 
 from table
where customer_id=given_customer_id --add filter if necessary
)s
where rn=1;
相关问题