需要有关SQL Server查询的帮助

时间:2011-05-13 15:18:41

标签: sql sql-server-2005 tsql

我想在SQL Server中编写此查询

from (
    select DISTINCT salary 
    from employee 
    order by salary desc
) 
where rownum = 3;

2 个答案:

答案 0 :(得分:7)

请参阅ROW_NUMBER()

,例如,

WITH EmployeeSalary AS
(
    select salary, 
        ROW_NUMBER() OVER (order by salary desc) AS 'RowNumber'
    FROM employee 
    group by salary --you can't use DISTINCT because ROW_NUMBER() makes each row distinct
) 
SELECT * 
FROM EmployeeSalary 
WHERE RowNumber = 3;

答案 1 :(得分:0)

SELECT DISTINCT salary 
FROM employee 
WHERE rownum = 3 
ORDER BY salary 

大写是可选的。 rownum是员工中的一列还是只查找返回的第三行?