select identity(int,1,1) as [sort],a.* into #t from (select * from userinfo where 学校='桥北二中' order by 分数 desc)tselect a.*,b.sort from userinfo a inner join #t b on a.分数=b.分数 where 学校='桥北二中'
select 分数,学生, ( select count(*) from userinfo where 学校 = u.学校 and 分数 > u.分数 ) + 1 as 名次 from userinfo u where 学校='桥北二中' order by 分数 desc
create table tb ( 学生 varchar(100), 分数 int, 学校 varchar(100) ) insert into tb select '李四',540,'桥北二中' union all select '张三',540,'桥北二中' union all select '王五',560,'桥北二中' union all select '李二',560,'桥北二中' union all select '李三',640,'桥北二中' union all select '李八',540,'桥北二中' union all select '大王',570,'实验二中'select c.学生,c.分数,c.学校,d.名次 from tb c join ( select 学校,分数,名次= (select count(1) from (select 学校,分数 from tb group by 学校,分数 ) b where b.学校=a.学校 and b.分数>=a.分数) from (select 学校,分数 from tb group by 学校,分数 ) a ) d on c.学校=d.学校 and c.分数=d.分数 order by c.学校,d.名次,c.学生学生 分数 学校 名次 ----------------------------------- 李三 640 桥北二中 1 李二 560 桥北二中 2 王五 560 桥北二中 2 李八 540 桥北二中 3 李四 540 桥北二中 3 张三 540 桥北二中 3 大王 570 实验二中 1 (所影响的行数为 7 行)
create table tb ( 学生 varchar(100), 分数 int, 学校 varchar(100) ) insert into tb select '李四',540,'桥北二中' union all select '张三',540,'桥北二中' union all select '王五',560,'桥北二中' union all select '李二',560,'桥北二中' union all select '李三',640,'桥北二中' union all select '李八',540,'桥北二中' union all select '大王',570,'实验二中'select *,(select count(*)+1 from (select distinct(分数) as 分数 from tb where 学校='桥北二中') t where t.分数>a.分数) as id from tb a where 学校='桥北二中' order by 分数 descdrop table tb
create table tb ( 学生 varchar(100), 分数 int, 学校 varchar(100) ) insert into tb select '李四',540,'桥北二中' union all select '张三',540,'桥北二中' union all select '王五',560,'桥北二中' union all select '李二',560,'桥北二中' union all select '李三',640,'桥北二中' union all select '李八',540,'桥北二中' union all select '大王',570,'实验二中'select *,(select count(*)+1 from (select distinct(分数) as 分数 from tb where tb.学校=a.学校) t where t.分数>a.分数) as id from tb a order by 学校 asc,分数 descdrop table tb
from (select * from userinfo where 学校='桥北二中' order by 分数 desc)tselect a.*,b.sort
from userinfo a
inner join #t b
on a.分数=b.分数
where 学校='桥北二中'
( select count(*)
from userinfo
where 学校 = u.学校 and 分数 > u.分数 ) + 1 as 名次
from userinfo u
where 学校='桥北二中'
order by 分数 desc
(
学生 varchar(100),
分数 int,
学校 varchar(100)
)
insert into tb
select '李四',540,'桥北二中' union all
select '张三',540,'桥北二中' union all
select '王五',560,'桥北二中' union all
select '李二',560,'桥北二中' union all
select '李三',640,'桥北二中' union all
select '李八',540,'桥北二中' union all
select '大王',570,'实验二中'select c.学生,c.分数,c.学校,d.名次 from tb c join (
select 学校,分数,名次=
(select count(1) from (select 学校,分数 from tb
group by 学校,分数
) b where b.学校=a.学校 and b.分数>=a.分数) from
(select 学校,分数 from tb
group by 学校,分数
) a
) d on c.学校=d.学校 and c.分数=d.分数
order by c.学校,d.名次,c.学生学生 分数 学校 名次
-----------------------------------
李三 640 桥北二中 1
李二 560 桥北二中 2
王五 560 桥北二中 2
李八 540 桥北二中 3
李四 540 桥北二中 3
张三 540 桥北二中 3
大王 570 实验二中 1 (所影响的行数为 7 行)
(
学生 varchar(100),
分数 int,
学校 varchar(100)
)
insert into tb
select '李四',540,'桥北二中' union all
select '张三',540,'桥北二中' union all
select '王五',560,'桥北二中' union all
select '李二',560,'桥北二中' union all
select '李三',640,'桥北二中' union all
select '李八',540,'桥北二中' union all
select '大王',570,'实验二中'select *,(select count(*)+1 from (select distinct(分数) as 分数 from tb where 学校='桥北二中') t where t.分数>a.分数) as id
from tb a
where 学校='桥北二中'
order by 分数 descdrop table tb
(
学生 varchar(100),
分数 int,
学校 varchar(100)
)
insert into tb
select '李四',540,'桥北二中' union all
select '张三',540,'桥北二中' union all
select '王五',560,'桥北二中' union all
select '李二',560,'桥北二中' union all
select '李三',640,'桥北二中' union all
select '李八',540,'桥北二中' union all
select '大王',570,'实验二中'select *,(select count(*)+1 from (select distinct(分数) as 分数 from tb where tb.学校=a.学校) t where t.分数>a.分数) as id
from tb a
order by 学校 asc,分数 descdrop table tb