如何从薪资表中找到第三或第n个最高薪水?

时间:2013-04-26 11:07:55

标签: sql sql-server sql-server-2008

如何以优化方式从薪水third or nth中找到table(EmpID,EmpName,EmpSalary)最高工资?

53 个答案:

答案 0 :(得分:85)

行号:

SELECT Salary,EmpName
FROM
  (
   SELECT Salary,EmpName,ROW_NUMBER() OVER(ORDER BY Salary) As RowNum
   FROM EMPLOYEE
   ) As A
WHERE A.RowNum IN (2,3)

子查询:

SELECT *
FROM Employee Emp1
WHERE (N-1) = (
               SELECT COUNT(DISTINCT(Emp2.Salary))
               FROM Employee Emp2
               WHERE Emp2.Salary > Emp1.Salary
               )

热门关键字:

SELECT TOP 1 salary
FROM (
      SELECT DISTINCT TOP n salary
      FROM employee
      ORDER BY salary DESC
      ) a
ORDER BY salary

答案 1 :(得分:72)

使用ROW_NUMBER(如果您需要单个)或DENSE_RANK(适用于所有相关行):

WITH CTE AS
(
    SELECT EmpID, EmpName, EmpSalary,
           RN = ROW_NUMBER() OVER (ORDER BY EmpSalary DESC)
    FROM dbo.Salary
)
SELECT EmpID, EmpName, EmpSalary
FROM CTE
WHERE RN = @NthRow

答案 2 :(得分:54)

试试这个

SELECT TOP 1 salary FROM (
   SELECT TOP 3 salary 
   FROM employees 
   ORDER BY salary DESC) AS emp 
ORDER BY salary ASC

对于3,您可以替换任何值...

答案 3 :(得分:37)

如果你想要优化方式意味着使用TOP关键字,那么第n个最大和最小工资查询如下,但是通过使用聚合函数名称查询看起来像一个棘手的反向顺序:

N最高工资:

SELECT MIN(EmpSalary)
FROM Salary
WHERE EmpSalary IN(SELECT TOP N EmpSalary FROM Salary ORDER BY EmpSalary DESC) 

表示:3:最高工资:

SELECT MIN(EmpSalary)
FROM Salary
WHERE EmpSalary IN(SELECT TOP 3 EmpSalary FROM Salary ORDER BY EmpSalary DESC) 

N最低工资:

SELECT MAX(EmpSalary)
FROM Salary
WHERE EmpSalary IN(SELECT TOP N EmpSalary FROM Salary ORDER BY EmpSalary ASC)

表示:3:最低工资:

SELECT MAX(EmpSalary)
FROM Salary
WHERE EmpSalary IN(SELECT TOP 3 EmpSalary FROM Salary ORDER BY EmpSalary ASC)

答案 4 :(得分:13)

如果你使用子查询太简单了!

SELECT MIN(EmpSalary) from (
SELECT EmpSalary from Employee ORDER BY EmpSalary DESC LIMIT 3
);

您可以在此处更改LIMIT约束后的第n个值。

这里的子查询选择EmpSalary DESC Limit 3中员工订单的EmpSalary;将返回员工的前3名工资。在结果中,我们将使用MIN命令选择最低工资来获得员工的第3个TOP工资。

答案 5 :(得分:12)

将N替换为最大数量

SELECT *
FROM Employee Emp1
WHERE (N-1) = (
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)

解释

如果您之前没有看到类似的内容,上面的查询会非常混乱 - 内部查询就是所谓的相关子查询,因为内部查询(子查询)使用外部查询中的值(在这种情况下)它的WHERE子句中的Emp1表。

Source

答案 6 :(得分:11)

不使用子查询的工资表中的第三或第n个最高工资

select salary from salary
   ORDER   BY salary DESC
   OFFSET  N-1 ROWS
   FETCH NEXT 1 ROWS ONLY

第3名最高薪水代替N-1

答案 7 :(得分:10)

SELECT Salary,EmpName
FROM
(
SELECT Salary,EmpName,DENSE_RANK() OVER(ORDER BY Salary DESC) Rno from EMPLOYEE
) tbl
WHERE Rno=3

答案 8 :(得分:6)

请参阅以下查询以获得第n个最高薪水。通过这种方式,你在MYSQL中获得第n个最高薪水。如果你想获得第n个最低工资,你需要在查询中用ASC替换DESC。 nth highest salary

答案 9 :(得分:5)

方法1:

SELECT TOP 1 salary FROM (
SELECT TOP 3 salary 
 FROM employees 
  ORDER BY salary DESC) AS emp 
 ORDER BY salary ASC

方法2:

  Select EmpName,salary from
  (
    select EmpName,salary ,Row_Number() over(order by salary desc) as rowid      
     from EmpTbl)
   as a where rowid=3

答案 10 :(得分:4)

SELECT EmpSalary 
FROM salary_table 
GROUP BY EmpSalary 
ORDER BY EmpSalary DESC LIMIT n-1, 1;

答案 11 :(得分:4)

2008年,我们可以使用ROW_NUMBER()OVER(ORDER BY EmpSalary DESC)来获得一个没有我们可以使用的关系的排名。

例如,我们可以通过这种方式获得第8高,或者将@N更改为其他内容,或者如果您愿意,可以将其用作函数中的参数。

DECLARE @N INT = 8;
WITH rankedSalaries AS
(
SELECT
EmpID
,EmpName
,EmpSalary,
,RN = ROW_NUMBER() OVER (ORDER BY EmpSalary DESC)
FROM salary
)
SELECT
EmpID
,EmpName
,EmpSalary
FROM rankedSalaries
WHERE RN = @N;

在SQL Server 2012中,您可能知道使用LAG()更直观地执行此操作。

答案 12 :(得分:3)

SELECT MIN(COLUMN_NAME)
FROM   (
           SELECT DISTINCT TOP 3     COLUMN_NAME
           FROM   TABLE_NAME
           ORDER BY
                  COLUMN_NAME        DESC
       ) AS 'COLUMN_NAME'

答案 13 :(得分:3)

Select TOP 1 Salary as '3rd Highest Salary' from (SELECT DISTINCT TOP 3 Salary from Employee ORDER BY Salary DESC) a ORDER BY Salary ASC;

我显示第三高薪

答案 14 :(得分:3)

- 第n个最高薪水

select * 
from (select lstName, salary, row_number() over( order by salary desc) as rn 
      from employee) tmp
where rn = 2

- (nth -1)最高薪水

select * 
from employee e1
where 1 = (select count(distinct salary)  
           from employee e2
           where e2.Salary > e1.Salary )

答案 15 :(得分:3)

这是任何SQL访谈中的热门问题之一。我将写下不同的查询以找出列的第n个最高值。

我通过运行以下脚本创建了一个名为“Emloyee”的表。

CREATE TABLE Employee([Eid] [float] NULL,[Ename] [nvarchar](255) NULL,[Basic_Sal] [float] NULL)

现在,我将通过运行下面的insert语句在该表中插入8行。

insert into Employee values(1,'Neeraj',45000)
insert into Employee values(2,'Ankit',5000)
insert into Employee values(3,'Akshay',6000)
insert into Employee values(4,'Ramesh',7600)
insert into Employee values(5,'Vikas',4000)
insert into Employee values(7,'Neha',8500)
insert into Employee values(8,'Shivika',4500)
insert into Employee values(9,'Tarun',9500)

现在我们将使用不同的查询从上表中找出第三高的Basic_sal。 我在管理工作室运行了以下查询,结果如下。

select * from Employee order by Basic_Sal desc

我们可以在上面的图像中看到第三高的基本工资是8500.我正在写3种不同的方式来做同样的事情。通过运行下面提到的所有三个查询,我们将得到相同的结果,即8500。

第一种方式: - 使用行号功能

select Ename,Basic_sal
from(
            select Ename,Basic_Sal,ROW_NUMBER() over (order by Basic_Sal desc) as rowid from Employee
      )A
where rowid=2

答案 16 :(得分:3)

declare @maxNthSal as nvarchar(20)
SELECT TOP 3 @maxNthSal=GRN_NAME FROM GRN_HDR   ORDER BY GRN_NAME DESC
print @maxNthSal

答案 17 :(得分:2)

优化方式:不使用子查询,而只使用限制。

{{1}}

请参阅此处的限制语法http://www.mysqltutorial.org/mysql-limit.aspx

答案 18 :(得分:2)

通过子查询:

struct NodeStruct *next;   //You need to use structure name 

答案 19 :(得分:1)

select * from employee order by salary desc;

+------+------+------+-----------+
| id   | name | age  | salary    |
+------+------+------+-----------+
|    5 | AJ   |   20 | 100000.00 |
|    4 | Ajay |   25 |  80000.00 |
|    2 | ASM  |   28 |  50000.00 |
|    3 | AM   |   22 |  50000.00 |
|    1 | AJ   |   24 |  30000.00 |
|    6 | Riu  |   20 |  20000.00 |
+------+------+------+-----------+




select distinct salary from employee e1 where (n) = (select count( distinct(salary) ) from employee e2 where e1.salary<=e2.salary);

将第n个最高薪水替换为n。

答案 20 :(得分:1)

尝试此查询

SELECT DISTINCT salary
FROM emp E WHERE
&no =(SELECT COUNT(DISTINCT salary) 
FROM emp WHERE E.salary <= salary)

把n =你想要的值

答案 21 :(得分:1)

在SQL Server 2012+中,OFFSET ... FETCH将是实现此目的的有效方法:

DECLARE @N AS INT;
SET @N = 3;

SELECT
    EmpSalary
FROM
    dbo.Salary
ORDER BY
    EmpSalary DESC
OFFSET (@N-1) ROWS
FETCH NEXT 1 ROWS ONLY

答案 22 :(得分:1)

尝试此代码:-

SELECT *
   FROM one one1
   WHERE ( n ) = ( SELECT COUNT( one2.salary )
                   FROM one one2
                   WHERE one2.salary >= one1.salary
                 )

答案 23 :(得分:1)

MySQL测试解决方案,假设N = 4:

select min(country) from (SELECT distinct country FROM Customers order by country desc limit 3);

另一个例子:

isNewUser()

答案 24 :(得分:1)

set @n = $n

SELECT a.* FROM ( select a.* , @rn = @rn+1  from EMPLOYEE order by a.EmpSalary desc ) As a  where rn = @n

答案 25 :(得分:0)

select 
    Min(salary) 
from ( select salary from employees order by salary desc) t
where rownum<=3;

对于第二高薪,在上述查询中将3改为2,将第N个最高薪水改为N,其中N = 1,2,3,4 ......

答案 26 :(得分:0)

你可以试试这个:

select top(1) EXPORT_NO
from DC_HDR 
order by CASE when  (ROW_NUMBER() over(order by EXPORT_NO desc))=3 then EXPORT_NO else 0 end desc

答案 27 :(得分:0)

根据日期查找最新最高数据的另一种方法

SELECT A.JID,A.EntryDate,RefundDate,Comments,Refund, ActionBy FROM (
(select JID, Max(EntryDate) AS EntryDate from refundrequested GROUP BY JID) A 
Inner JOIN (SELECT JID,ENTRYDATE,refundDate,Comments,refund,ActionBy from refundrequested) B 
ON A.JID=B.JID AND A.EntryDate = B.EntryDate) 

答案 28 :(得分:0)

显示所有第三高薪:

select * from emp where sal=
(SELECT DISTINCT sal FROM emp ORDER BY sal DESC LIMIT 3,1) ;

仅显示第三高薪:

SELECT DISTINCT sal FROM emp ORDER BY sal DESC LIMIT 3,1

答案 29 :(得分:0)

要使用AdventureWorks2012查询nth highest bonus,说n=10,请尝试以下代码

USE AdventureWorks2012; 
GO

SELECT * FROM Sales.SalesPerson;
GO

DECLARE @grade INT;
SET @grade = 10;
SELECT MIN(Bonus)
FROM (SELECT TOP (@grade) Bonus FROM (SELECT DISTINCT(Bonus) FROM Sales.SalesPerson) AS a ORDER BY Bonus DESC) AS g

答案 30 :(得分:0)

使用CTE和FIRST_VALUE函数查找第N个最大值。 -最高工资的第五名

;WITH CTE_NTH_SAL AS 
    (SELECT FIRST_VALUE(ESAL) OVER(ORDER BY ESAL DESC) AS ESAL,
        1 AS ID
    FROM EMPLOYEE
    UNION ALL
    SELECT FIRST_VALUE(EMP.ESAL) OVER(ORDER BY EMP.ESAL DESC) AS ESAL,
        ID
    FROM EMPLOYEE EMP,
        (SELECT ESAL,
        ID+1 AS ID
        FROM CTE_NTH_SAL) CTE_NTH_SAL
        WHERE EMP.ESAL<CTE_NTH_SAL.ESAL
                AND CTE_NTH_SAL.ID<=5 )
    SELECT DISTINCT ESAL
FROM CTE_NTH_SAL
WHERE ID=5

获取示例结果集和更多方法clickhere

答案 31 :(得分:0)

从表中找到N最高薪水。这是一种使用density_rank()函数完成此任务的方法。

enter image description here

select linkorder from u_links

select max(linkorder) from u_links

select max(linkorder) from u_links where linkorder < (select max(linkorder) from u_links)

select top 1 linkorder 
       from ( select distinct top 2 linkorder from u_links order by linkorder desc) tmp 
order by linkorder asc

DENSE_RANK: 1. DENSE_RANK计算行的有序组中行的等级,并将等级返回为NUMBER。等级是从1开始的连续整数。 2.此函数接受参数作为任何数字数据类型,并返回NUMBER。 3.作为一种分析功能,DENSE_RANK基于order_by_clause中value_exprs的值,计算从查询返回的每一行相对于其他行的排名。 4.在上面的查询中,将根据employee表的sal返回等级。如果出现平局,它将为所有行分配相等的等级。

WITH result AS ( 
     SELECT linkorder ,DENSE_RANK() OVER ( ORDER BY linkorder DESC ) AS  DanseRank 
FROM u_links ) 
SELECT TOP 1 linkorder FROM result WHERE DanseRank = 5

答案 32 :(得分:0)

试试这个...

SELECT MAX(salary) FROM employee WHERE salary NOT IN (SELECT * FROM employee ORDERBY salary DESC LIMIT n-1)

答案 33 :(得分:0)

子查询总是需要更多时间:

使用以下查询获取任何最高和最低数据:

最高数据:select *from business order by id desc limit 3,1;

最低数据:select *from business order by id asc limit 3,1;

可以使用N代替3来获取第n个数据。

答案 34 :(得分:0)

注意:请使用任意第N个整数替换 OFFSET 3

SELECT EmpName,EmpSalary
FROM SALARY
ORDER BY EmpSalary DESC
OFFSET 3 ROWS 
FETCH NEXT 1 ROWS ONLY

说明

  

FETCH NEXT 1 ROWS ONLY

只返回1行

  

OFFSET 3 ROWS

排除前3条记录在这里你可以得到任何整数

答案 35 :(得分:0)

int size = (int) (srcMat.total() * srcMat.channels());

答案 36 :(得分:0)

SELECT * FROM(按工资DESC从客户订单中选择不同的工资)限制4,1;

限制4,1表示先离开前4行,然后选择下一行。

限制和rownumber取决于您使用的平台。

试试这个,它会起作用。

答案 37 :(得分:-1)

找到第五高薪:

Declare @N INT = 5 
SELECT Salary FROM Employee 
ORDER BY Salary DESC OFFSET @N - 1 ROW

答案 38 :(得分:-1)

您好再使用公用表表达式编写此示例:

 with cte as
(SELECT TOP 3 salary 
   FROM salary
   ORDER BY salary DESC)
   select top 1 salary from cte

   with cte as
(select e.Name,s.salary ,DENSE_RANK ()over ( order by salary desc) sal_rank 
from salary s left join employee e
on s.EmpID=e.EmpID)
select Name,salary from cte where sal_rank=3

答案 39 :(得分:-1)

//你可以从table中找到第n个工资。如果你想要获得第二高薪然后把n = 2,如果第3个小时然后输出n = 3那么......

SELECT *  FROM tablename t1 
WHERE (N-1) = (SELECT COUNT(DISTINCT(t2.Salary)) 
 FROM tablename t2
WHERE t2.Salary > t1.Salary)

答案 40 :(得分:-1)

SELECT TOP 1 salary 
FROM   employee 
WHERE  salary IN (SELECT DISTINCT TOP 3 salary 
                  FROM   employee 
                  ORDER  BY salary DESC) 
ORDER  BY salary ASC 

答案 41 :(得分:-1)

select max(sal) 
from emp 
where sal > (
    select max(sal) 
    from emp 
    where sal > (select max(sal) from emp)
);

答案 42 :(得分:-1)

SELECT TOP 1 salary FROM ( SELECT TOP n salary FROM employees ORDER BY salary DESC Group By salary ) AS emp ORDER BY salary ASC

(其中n表示第n个最高薪水)

答案 43 :(得分:-1)

优化方式,

;With CTE as
(
select salary 
        ,RANK(order by salary desc) as Rnk
from table
)
select * from CTE where Rnk = @yourVariable

第二意见,

Declare @yourVariable int = 2
Select top 1 * from 
(
select distinct top(@yourVariable)  salary 
from Employees
order by salary desc
)as a
order by a.salary asc
select * from Employees
order by salary desc

答案 44 :(得分:-1)

只需更改内部查询值:E.g按ClassID desc从Student_Info顺序中选择Top(2)*

用于这两个问题:

Select Top (1)* from 
(
 Select Top (1)* from Student_Info order by ClassID desc 
) as wsdwe
order by ClassID 

答案 45 :(得分:-2)

SELECT TOP 1 salary FROM (
   SELECT TOP 3 salary 
   FROM employees 
   Group By salary ORDER BY salary DESC ) AS emp 
ORDER BY salary ASC

答案 46 :(得分:-2)

select *  from emp x where &no=(select count(*) from emp y where y.sal>=x.sal);

这将提供来自用户的接受输入,然后将告诉第n个最大数字。我已经在oracle中获取了emp表的示例并显示了第n个最大受薪员工信息

输出 输入no:5

的值
 EMPNO ENAME      JOB       MGR        HIREDATE  SAL        COMM       DEPTNO
 ----- ---------- --------- ---------- --------- ---------- ---------- ----------
 7698  BLAKE      MANAGER   7839       01-MAY-81 3000                  30
 7788  SCOTT      ANALYST   7566       19-APR-87 3000                  20
 7902  FORD       ANALYST   7566       03-DEC-81 3000                  20

输入no:14

的值
 EMPNO ENAME      JOB       MGR        HIREDATE  SAL        COMM       DEPTNO
 ----- ---------- --------- ---------- --------- ---------- ---------- ----------
 7369  SMITH      CLERK     7902       17-DEC-80 800                   20

答案 47 :(得分:-2)

这是用于从表emp中获取第n个最高薪水的mysql查询。 我们在这里使用“LIMIT”作为“TOP”的替代,参数(n-1)显示从哪一行开始,第二个参数显示从第(n-1)行开始显示的行数。 “LIMIT”也接受单个参数,它也表示从第0个(或第1个)开始显示的行数。

select distinct * from emp order by sal desc limit (n-1),1;

答案 48 :(得分:-2)

declare @nHighestSalary as int
set @nHighestSalary = 3
SELECT TOP 1 salary FROM (
   SELECT TOP @nHighestSalary salary 
   FROM employees 
   ORDER BY salary DESC) AS emp 
ORDER BY salary ASC

答案 49 :(得分:-2)

获得第n个最高薪水。

SELECT DISTINCT Salary
FROM EMP E WHERE
n =(SELECT COUNT(DISTINCT SALARY) 
FROM EMP WHERE E.SALARY <= SALARY)

n是您想要的最高值,即2,3等

答案 50 :(得分:-2)

SELECT * /*This is the outer query part */
FROM Employee Emp1
WHERE (N-1) = ( /* Subquery starts here */
SELECT COUNT(DISTINCT(Emp2.Salary))
FROM Employee Emp2
WHERE Emp2.Salary > Emp1.Salary)

答案 51 :(得分:-2)

对于n- th 最高值

select min(salary) 
from (select salary from(select salary from employee order by salary desc)where rownum<=n);

答案 52 :(得分:-3)

select  a.GRN_NAME 
from GRN_HDR a,GRN_HDR b 
where a.GRN_NAME<=b.GRN_NAME 
group by a.GRN_NAME 
having count(a.GRN_NAME)=3