在SQl中获得员工表中第二高工资的查询是什么? [英] What is the query to get second highest salary in employee table in SQl?

查看:399
本文介绍了在SQl中获得员工表中第二高工资的查询是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得第二高薪或者说第n个最高薪水.....请帮助?

如何获得前n个最高工资的记录?

解决方案

试试这个。

选择* from(选择ROW_NUMBER()OVER(ORDER BY Salary DESC)作为ID,*来自表格)as t其中ID = n 



n - 工资位置


如果你可以有重复项(同一个工资超过一次),一种方法是:

 选择 c。* 
来自选择 b.onetime,
row_number() over 订单 b.onetime desc )的位置
来自选择 不同
a.salary as 一次性
来自 sometable a)b)c
其中 c.position = 2





如果你想得到例如前5名工资然后改变条件:

 ... 其中 c.position< =  5  


在这里。其他替补



第二高价值 [ ^

I want to get the second highest salary or say nth highest salary.....pls help?
also how to get record of first n highest salaries?

解决方案

Try this.

Select * from (select  ROW_NUMBER() OVER (ORDER BY Salary DESC) as ID, * from table) as t where ID = n


n - position of salary


If you can have duplicates (the same salary more than one time), one way is:

select c.*
from (select b.onetime, 
             row_number() over (order by b.onetime desc) position
      from (select distinct 
                   a.salary as onetime 
            from   sometable a) b) c
where c.position = 2



if you want yo get for example top 5 salaries then change the condition:

...where c.position <= 5


Here you go. Other Alternates

Second highest value[^]


这篇关于在SQl中获得员工表中第二高工资的查询是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆