English 中文(简体)
如何在服务器中找到顶级浏览器
原标题:SQL query to find the top rows in SQL Server
  • 时间:2012-04-13 11:05:39
  •  标签:
  • sql-server

在库克服务器中,我正设法获得5名最高工资。

我的薪水一样。

5000
5000
4500
4500
3000
2000 
1000
500
400

和我一样

5000
5000
4500
4500
3000
2000
1000 
最佳回答

如果你想获得五位最高的单独薪金(无论数额多少倍可能显示),你需要使用<条码> DENSE_RANK(的排名职能和一个计算中心来做到这一点:

DECLARE @salaries TABLE (salary DECIMAL(18,4))

INSERT INTO @salaries VALUES(5000)
INSERT INTO @salaries VALUES(5000)
INSERT INTO @salaries VALUES(4500)
INSERT INTO @salaries VALUES(4500)
INSERT INTO @salaries VALUES(3000)
INSERT INTO @salaries VALUES(2000)
INSERT INTO @salaries VALUES(1000)
INSERT INTO @salaries VALUES(500)
INSERT INTO @salaries VALUES(400)

;WITH SalariesRanked AS
(
    SELECT 
        Salary, 
        SalaryNumber = DENSE_RANK() OVER(ORDER BY Salary DESC)
    FROM    
        @salaries
)
SELECT salary
FROM SalariesRanked 
WHERE SalaryNumber <= 5

结果是产生了这样的产出:

salary
5000.0000
5000.0000
4500.0000
4500.0000
3000.0000
2000.0000
1000.0000
问题回答
SELECT TOP 5 salary FROM your_table
ORDER BY salary DESC
select salary
from
  (
    select salary,
           dense_rank() over(order by salary desc) as rn
    from YourTable
  ) as T
where rn <= 5

Try on SE-Data

SELECT TOP 5 Salary
FROM [Table]
GROUP BY Salary
ORDER BY Salary DESC

你们需要把问题分类,以避免重复的工资。





相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签