如何从工资表中find第三或第n个最高工资?

如何以优化方式从工资table(EmpID,EmpName,EmpSalary)中findthird or nth最高工资?

使用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 

行号:

 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 

尝试这个

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

对于3,你可以取代任何值…

如果你想优化方式意味着使用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) 
 SELECT Salary,EmpName FROM ( SELECT Salary,EmpName,DENSE_RANK() OVER(ORDER BY Salary DESC) Rno from EMPLOYEE ) tbl WHERE Rno=3 

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

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

你可以在这里改变LIMIT约束之后的第n个值。

在这里,子查询从EmpSalary DESC Limit 3中selectEmployee Order中的EmpSalary; 将返回员工的前三名工资。 结果,我们将select使用MIN命令的最低工资来获得员工的第三个最高工资。

没有使用子查询,从工资表中扣除第三或第n个最高工资

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

对于第三高的薪水,把2代替N-1

用你的最大数字replaceN.

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

说明

上面的查询可能会相当混乱,如果你之前没有看到过这样的内容 – 内部查询就是所谓的相关子查询,因为内部查询(子查询)使用来自外部查询的值(在这种情况下是Emp1表)在它的WHERE子句中。

来源

方法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 

在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()更直观地执行的。

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

 SELECT EmpSalary FROM salary_table GROUP BY EmpSalary ORDER BY EmpSalary DESC LIMIT $nth ,1; 
 declare @maxNthSal as nvarchar(20) SELECT TOP 3 @maxNthSal=GRN_NAME FROM GRN_HDR ORDER BY GRN_NAME DESC print @maxNthSal 

这是任何SQL面试中stream行的问题之一。 我将写下不同的查询来找出列的第n个最高值。

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

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

现在我要通过运行下面的插入语句插入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。 我已经在pipe理工作室中运行以下查询,下面是结果。

 select * from Employee order by Basic_Sal desc 

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

第一种方式: – 使用行号function

 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 
 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; 

我显示了第三高的薪水

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

– 最高的工资

 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 ) 

通过子查询:

 SELECT salary from (SELECT rownum ID, EmpSalary salary from (SELECT DISTINCT EmpSalary from salary_table order by EmpSalary DESC) where ID = nth) 

试试这个查询

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

把n =你想要的值

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

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

 select distinct salary from employee order by salary desc limit nth, 1; 

请参阅限制语法在这里http://www.mysqltutorial.org/mysql-limit.aspx

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

 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 

另一种基于date查找最高数据的方法

 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) 

你可以试试这个:

 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 
 select min(salary) from (select salary from employee where rownum < n+1 order by salary desc); 

显示所有第三高的薪水:

 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 
 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个最高工资)

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

 select distinct * from emp order by sal desc limit (n-1),1; 
 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 

只需更改内部查询值:例如,按照ClassID desc从Student_Info的顺序selectTop(2)*

用于这两个问题:

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