34,837
社区成员




drop table tb
CREATE TABLE tb(Name varchar(10),Score decimal(10,2))
INSERT tb SELECT 'aa',99
UNION ALL SELECT 'bb',56
UNION ALL SELECT 'cc',56
UNION ALL SELECT 'dd',77
UNION ALL SELECT 'ee',78
UNION ALL SELECT 'ff',76
UNION ALL SELECT 'gg',78
UNION ALL SELECT 'ff',50
GO
---当分数相同时,合并名次
SELECT *,Place=(SELECT COUNT(DISTINCT Score) FROM tb WHERE Score>=a.Score)
FROM tb a
--WHERE NAME='001'
ORDER BY Place
---当分数相同时,保留名次空缺
SELECT *,Place=(SELECT COUNT(Score) FROM tb WHERE Score>a.Score)+1
FROM tb a
--WHERE NAME='001'
ORDER BY Place
---------------------
Name Score Place
---------- --------------------------------------- -----------
aa 99.00 1
ee 78.00 2
gg 78.00 2
dd 77.00 3
ff 76.00 4
bb 56.00 5
cc 56.00 5
ff 50.00 6
(8 行受影响)
Name Score Place
---------- --------------------------------------- -----------
aa 99.00 1
ee 78.00 2
gg 78.00 2
dd 77.00 4
ff 76.00 5
bb 56.00 6
cc 56.00 6
ff 50.00 8
(8 行受影响)
select *,px=identity(int,1,1)
into #tb
from tb
order by score desc
select *
from #tb
where no = '001'
select *
from (
select *,px=row_number() over (order by score desc)
from [table]
)
where no = '001'
select row_number() over (order by socre desc) as 名次, no,socre
from table where no='001' order by socre desc