--sql 2005以上 select * from ( select *,名次=ROW_NUMBER() OVER(ORDER BY 分数 desc) from tb )t order by 名次 --sql 2000 select * from ( select *,名次=(select count(*) from tb b where b.分数>=a.分数 from tb a )t order by 名次
--sql 2000 create table #tb(姓名 varchar(10),分数 int) insert into #tb select '张三',87 union all select '张四',98 union all select '李三',83 union all select '李四',85select * from ( select *,名次=(select count(*) from #tb b where b.分数>=a.分数) from #tb a )t order by 名次/* 姓名 分数 --------------------- 张四 98 1 张三 87 2 李四 85 3 李三 83 4*/--sql 2005以上 select * from ( select *,名次=RANK() OVER(ORDER BY 分数 desc) from tb -->应该用RANK()函数,分数相同就并列 )t order by 名次
--sql 2005以上
select *
from
(
select *,名次=ROW_NUMBER() OVER(ORDER BY 分数 desc) from tb
)t
order by 名次
--sql 2000
select *
from
(
select *,名次=(select count(*) from tb b where b.分数>=a.分数
from tb a
)t
order by 名次
create table #tb(姓名 varchar(10),分数 int)
insert into #tb
select '张三',87
union all select '张四',98
union all select '李三',83
union all select '李四',85select *
from
(
select *,名次=(select count(*) from #tb b where b.分数>=a.分数)
from #tb a
)t
order by 名次/*
姓名 分数
---------------------
张四 98 1
张三 87 2
李四 85 3
李三 83 4*/--sql 2005以上
select *
from
(
select *,名次=RANK() OVER(ORDER BY 分数 desc) from tb -->应该用RANK()函数,分数相同就并列
)t
order by 名次