select
DENSE_RANK() over (order by score desc) as Rank ,
username ,
score
from users
我尝试了以上查询,结果如下:
Rank Username score
----- -------- -----
1 A 3500
1 B 3500
2 C 3000
2 D 3000
2 E 3000
3 F 2000
而我的要求是:
Rank Username score
----- -------- -----
1 A 3500
1 B 3500
3 C 3000
3 D 3000
3 E 3000
6 F 2000
如何编辑我的查询以获得所需的结果?
答案 0 :(得分:1)
使用Rank()
代替Dense_Rank()
将解决您的问题。
SELECT
RANK() OVER (ORDER BY Score DESC) AS [Rank],
Username,
Score
FROM Users
使用给定的样本数据执行执行:
DECLARE @Users TABLE (Username VARCHAR (2), Score INT)
INSERT INTO @Users (Username, Score)
VALUES
('A', 3500),
('B', 3500),
('C', 3000),
('D', 3000),
('E', 3000),
('F', 2000)
SELECT
RANK() OVER (ORDER BY Score DESC) AS [Rank],
Username,
Score
FROM @Users
所以输出将是:
Rank UserName Score
---- -------- ------
1 A 3500
1 B 3500
3 C 3000
3 D 3000
3 E 3000
6 F 2000