如何找到第二高薪
问题描述:
我有一个名称为
的表
tbPay
现在有很多不同净额的工资
现在我只想获得第二高的薪水金额.
I have a table with name
tbPay
Now there are alot of pays with differnet amount
Now i want to get the 2nd highest pay amount only.
答
看这篇文章:http://blog.sqlauthority.com/2008/04 /02/sql-server-find-nth-nest-salary-of-employee-query-to-retrieve-the-nth-maximum-value/ [
Look at this article: http://blog.sqlauthority.com/2008/04/02/sql-server-find-nth-highest-salary-of-employee-query-to-retrieve-the-nth-maximum-value/[^]
SELECT
TOP 1 salary
FROM
(SELECT DISTINCT TOP 2 salary FROM tbPay ORDER BY salary DESC) a ORDER BY salary
尝试:
WITH Results as
(SELECT Row_Number() over (ORDER BY Salary DESC) as RN,* FROM tbPay)
SELECT * FROM Results WHERE RN=2
尝试以下查询.salary
是包含员工薪水的列,而tbPay
是包含员工记录的表的名称.
Try the following query.
Thesalary
is the column which contains the employees salary and thetbPay
is the name of the table which has the records of the employees.
SELECT MAX(salary) FROM tbPay WHERE salary NOT IN (SELECT MAX(salary) FROM tbPay)
这有帮助吗?
Does this help?