select 姓名,排名=(select sum(1) from @t where 成绩>=a.成绩 and (成绩1-成绩2)>=(a.成绩1-a.成绩2)) from 表 a order by 成绩 desc,成绩1-成绩2 desc
--测试数据 declare @t table(姓名 varchar(10),成绩 int,成绩1 int,成绩2 int) insert into @t select '张一',90,100,40 union all select '张二',80,80,50 union all select '张三',85,100,40 union all select '张四',85,100,50--查询 select 姓名 ,排名=(select sum(1) from @t where 成绩>=a.成绩 and (成绩1-成绩2)>=(a.成绩1-a.成绩2)) from @t a order by 成绩 desc,成绩1-成绩2 desc /*--测试结果 姓名 排名 ---------- ----------- 张一 1 张三 2 张四 3 张二 4(所影响的行数为 4 行) --*/
from 表 a
order by 成绩 desc,成绩1-成绩2 desc
declare @t table(姓名 varchar(10),成绩 int,成绩1 int,成绩2 int)
insert into @t
select '张一',90,100,40
union all select '张二',80,80,50
union all select '张三',85,100,40
union all select '张四',85,100,50--查询
select 姓名
,排名=(select sum(1) from @t where 成绩>=a.成绩 and (成绩1-成绩2)>=(a.成绩1-a.成绩2))
from @t a
order by 成绩 desc,成绩1-成绩2 desc
/*--测试结果
姓名 排名
---------- -----------
张一 1
张三 2
张四 3
张二 4(所影响的行数为 4 行)
--*/