日期:2014-05-18  浏览次数:20462 次

关于名次排列的问题
现在是这样的.我做一个名次排行.是关于选手得票数的,想问下大家用SQL语句排序之后如何才能确定同名次的,也就比如说A和B都得10票,并列排名第三,这样该如何做.我用的是datalist控件绑定字段。

------解决方案--------------------
select 数量,(select count(*)+1 as 排名 from 表名 b where b.数量>a.数量) as 排名 from 表名 a order by 数量 desc
------解决方案--------------------
根据baobei7758的思路, 我写了个测试DEMO
--准备数据
CREATE TABLE ScoreList
(
UserId VARCHAR(10) PRIMARY KEY NOT NULL,
Score INT 
)
GO

INSERT [ScoreList] (
[UserId],
[Score]

SELECT 'xiaoluo', 40
UNION ALL SELECT 'xiaoli', 20
UNION ALL SELECT 'xiaoliu', 40
UNION ALL SELECT 'xiaoliang', 45

--查询1
select UserId AS [姓名], Score AS [分数],
(select count(*)+1 as [Order] from ScoreList b where b.Score>a.Score) as [名次] 
from ScoreList a order by Score DESC

------结果是:
姓名 分数 名次
xiaoliang 45 1
xiaoliu 40 2
xiaoluo 40 2
xiaoli 20 4

--查询2
select UserId AS [姓名], Score AS [分数],
(select count(DISTINCT [Score])+1 as [Order] from ScoreList b where
b.Score>a.Score) as [名次] 
from ScoreList a order by Score DESC

------结果是:
姓名 分数 名次
xiaoliang 45 1
xiaoliu 40 2
xiaoluo 40 2
xiaoli 20 3