在Presto SQL中选择Top#?

时间:2016-06-06 21:36:20

标签: sql presto

Presto SQL在SELECT语句中是否真的缺少TOP X功能?

如果是这样,同时有解决方法吗?

https://prestodb.io/

1 个答案:

答案 0 :(得分:10)

如果您只想限制结果集中的行数,可以使用LIMIT,有或没有ORDER BY

SELECT department, salary
FROM employees
ORDER BY salary DESC
LIMIT 10

如果要为每个组提供最高值,可以使用标准SQL row_number()窗口函数。例如,按薪水获得每个部门的前3名员工:

SELECT department, salary
FROM (
  SELECT department, salary row_number() OVER (
    PARTITION BY department
    ORDER BY salary DESC) AS rn
  FROM employees
)
WHERE rn <= 3