gpt4 book ai didi

sql - 在 Presto SQL 中选择 Top #?

转载 作者:行者123 更新时间:2023-12-03 14:36:04 26 4
gpt4 key购买 nike

Presto SQL 真的缺少 SELECT 语句中的 TOP X 功能吗?

如果是这样,在此期间是否有解决方法?

https://prestodb.io/

最佳答案

如果只是想限制结果集中的行数,可以使用 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

关于sql - 在 Presto SQL 中选择 Top #?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37667265/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com