gpt4 book ai didi

sql - 查询以查找列的 nᵗʰ 最大值

转载 作者:太空狗 更新时间:2023-10-30 01:39:38 26 4
gpt4 key购买 nike

我想找到一列的第 2nd、第 3rd ... n 个最大值。

最佳答案

考虑以下具有单个薪水列的 Employee 表。

+------+| Sal  |+------+| 3500 | | 2500 | | 2500 | | 5500 || 7500 |+------+

The following query will return the Nth Maximum element.

select SAL from EMPLOYEE E1 where 
(N - 1) = (select count(distinct(SAL))
from EMPLOYEE E2
where E2.SAL > E1.SAL )

例如。当需要第二个最大值时,

  select SAL from EMPLOYEE E1 where 
(2 - 1) = (select count(distinct(SAL))
from EMPLOYEE E2
where E2.SAL > E1.SAL )
+------+| Sal  |+------+| 5500 |+------+

关于sql - 查询以查找列的 nᵗʰ 最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/80706/

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