sql - Hive SQL 排名与关系
问题描述
如何在Hive SQL 中重新编写以下查询
select * from sales order by unit_price fetch with ties
解决方案
Oracle 的 ORDER BY .. FETCH NEXT N ROWS WITH TIRES 用于限制返回的 top N(使用 ORDER BY)行数为 NEXT(FIRST)指定的数量。而轮胎意味着如果某些行具有相同的值顺序,除了指定的行数之外,它们也将被返回。
Hive 没有 FETCH 功能。有LIMIT,不支持WITH TIRES。
您可以使用分析 dense_rank() 函数加上 WHERE 过滤器来实现类似的功能。例如,我们需要获取 5 个最低价格的销售,如果有相同价格的销售,也返回它们。dense_rank 将为具有相同价格的行分配相同的排名。
select *
from
(
select s.*,
dense_rank() over(order by unit_price) rnk --lowest unit_price first
--to get highest price, use order by unit_price DESC
from sales s
)
where rnk<=5 --If there are rows with the same rank, they all will be returned
order by rnk
推荐阅读
- unity3d - Unity MRTK with HoloLens 2:如何在代码中检测手是否触摸了游戏对象?
- javascript - 如何将 ExpressJS 路由器传递到隔离虚拟机实例
- python - 如何让驱动程序导航到 selenium python 中的新页面
- javascript - TypeError:无法读取未定义的属性“参数”以更新类别
- flutter - 在 LicensePage 中隐藏我的颤振应用程序中的包
- discord.py - 使用 tatsu API 时对象没有属性
- react-native - React Native:[Branch] customMetadata 值必须是字符串
- python - 如果用户不采取行动功能被省略
- java - 无法在 ftl 文件中映射 jsonRequest 属性
- amazon-web-services - 我正在使用 AWS Cognito 服务,我正在尝试在登录时删除 OTP