日期:2014-05-17 浏览次数:20525 次
with tb(score)
as(
select 800 union all
select 200 union all
select 300 union all
select 800 union all
select 700 union all
select 300 union all
select 800
)
SELECT RANK()OVER(ORDER BY score DESC )[rank],score
FROM tb
/*
rank score
-------------------- -----------
1 800
1 800
1 800
4 700
5 300
5 300
7 200
(7 行受影响)
*/
--> 测试数据:#tb
IF OBJECT_ID('TEMPDB.DBO.#tb') IS NOT NULL DROP TABLE #tb
GO
CREATE TABLE #tb([socre] INT)
INSERT #tb
SELECT 800 UNION ALL
SELECT 700 UNION ALL
SELECT 700 UNION ALL
SELECT 500 UNION ALL
SELECT 500 UNION ALL
SELECT 200
--------------开始查询--------------------------
SELECT *,[rank]=DENSE_RANK()OVER(ORDER BY [socre] DESC ) FROM #tb
----------------结果----------------------------
/*
socre rank
800 1
700 2
700 2
500 3
500 3
200 4
*/