2008-12-11 165 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 

它工作正常,請使用它。

相關問題