2008-12-11 136 views

回答

22

在SQL Server 2005 & 2008年,创建一个排名子查询的查询,然后添加一个地方where子句中的排名= 5

select 
    * 
from 
(
    Select 
    SalesOrderID, CustomerID, Row_Number() Over (Order By SalesOrderID) as RunningCount 
    From 
    Sales.SalesOrderHeader 
    Where 
    SalesOrderID > 10000 
    Order By 
    SalesOrderID 
) ranked 
where 
    RunningCount = 5 
4

在SQL Server 2000

DECLARE @result int 

SELECT TOP 5 @result = Salary FROM Employees ORDER BY Salary DESC 

语法这些工作应该接近。我目前无法测试它。

或者你可以使用子查询去:

SELECT MIN(Salary) FROM (
    SELECT TOP 5 Salary FROM Employees ORDER BY Salary DESC 
) AS TopFive 

再次,如果语法是完全正确的,但是这种方法的工作原理并不积极。

+1

我希望它在一个单一的查询使用百分比...如何得到它? 从表名 选择排名前5%的列名 order by desc 使用此我们得到前5条记录,但我只想要第5条记录。 – Yogini 2008-12-11 06:45:10

+0

这两个查询都返回一个单一的数字,这是第五高的薪水。 你试过了吗? – recursive 2008-12-11 19:30:04

0

你可以尝试一些事情,如:

select salary 
from Employees a 
where 5=(select count(distinct salary) 
     from Employees b 
     where a.salary > b.salary) 
order by salary desc 
1
SELECT TOP 1 salary 
FROM (
    SELECT DISTINCT TOP n salary 
    FROM employee 
    ORDER BY salary DESC) a 
ORDER BY salary 
where n > 1 -- (n is always greater than one) 

您可以使用此查询任意数量的最高薪水。

0

您可以使用此查询发现:

select top 1 salary 
from (select top 5 salary 
     from tbl_Employee 
     order by salary desc) as tbl 
order by salary asc 
0

下面的查询,以获得特定员工姓名后最高的薪水。

只是有一个看看!

SELECT TOP 1 salary FROM (
    SELECT DISTINCT min(salary) salary 
    FROM emp where salary > (select salary from emp where empname = 'John Hell') 
    ) a 
ORDER BY salary 
0
select * from employee2 e 
where 2=(select count(distinct salary) from employee2 
     where e.salary<=salary) 

其工作

1

从数据库找到5 higest工资,查询是..

select MIN(esal) from (
    select top 5 esal from tbemp order by esal desc) as sal 

其工作检查出来

1
SELECT MIN(Salary) FROM (
    SELECT TOP 2 Salary FROM empa ORDER BY Salary DESC 
) AS TopFive 

它工作正常,请使用它。

相关问题