求一条sql排名语句

joehunter 2009-03-15 11:22:24
表结构如下:
考试ID|学生ID|班级|姓名|成绩
希望得出:
1.某次考试(由考试ID决定)中,成绩排名(不同的学生分数一样时排名一样,如:1,2,2,4...);
2.某次考试(由考试ID决定)中,全年级前十名在各班级的分布情况(即1班有多少个,2班有多少个...?)
多谢帮助,不甚感激!
...全文
557 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
joehunter 2009-03-17
  • 打赏
  • 举报
回复
对不住大家啊,我的分太少了
joehunter 2009-03-17
  • 打赏
  • 举报
回复
感谢大家,我得好生学习
中国风 2009-03-15
  • 打赏
  • 举报
回复
漏了考试ID,海兄是正確的
--SQL2000:

select *,[成绩排名]=(select count( 成绩) from table where 成绩>t.成绩 and 考试ID=t.考试ID)+1
from table t

--橫顯示分佈
select
[班级1]=sum(case when 班级=1 then 1 else 0 end),
[班级2]=sum(case when 班级=2 then 1 else 0 end),
[班级3]=sum(case when 班级=4 then 1 else 0 end),
................

from
(select *,(select count( 成绩) from table where 成绩>t.成绩 and 考试ID=t.考试ID)+1 as [成绩排名] from table t)t1
where [成绩排名]<=10

--豎顯示
select
班级,count([成绩排名]) as 分佈
from
(select *,(select count( 成绩) from table where 成绩>t.成绩 and 考试ID=t.考试ID)+1 as [成绩排名] from table t)t1
where [成绩排名]<=10
昵称被占用了 2009-03-15
  • 打赏
  • 举报
回复
2005
2
;with cte (班级,排名 ) as (
select 班级,RANK() OVER (partition by 考试ID order by 成绩 desc) as 排名
from tb
)
select 班级,count(1)
from cte
where 排名<= 10
group by 班级
昵称被占用了 2009-03-15
  • 打赏
  • 举报
回复
2005
select *,RANK() OVER (partition by 考试ID order by 成绩 desc) as 排名 
from tb
昵称被占用了 2009-03-15
  • 打赏
  • 举报
回复
2000
1
select a.*,1+isnull((select count(1) from tb where 考试ID= a.考试ID and 成绩<a.成绩),0) as 排名 
from tb a
order by a.成绩 desc
rucypli 2009-03-15
  • 打赏
  • 举报
回复
2
select 班级,count(*)
from
(
select top 10 *
from tb
order by 成绩 desc
)T
group by 班级
中国风 2009-03-15
  • 打赏
  • 举报
回复
SQL2000:
select *,[成绩排名]=(select count( 成绩) from table where 成绩>t.成绩)+1
from table t

select
[班级1]=sum(case when 班级=1 then 1 else 0 end),
[班级2]=sum(case when 班级=2 then 1 else 0 end),
[班级3]=sum(case when 班级=4 then 1 else 0 end),
................

from
(select *,(select count( 成绩) from table where 成绩>t.成绩)+1 from table t)t1
where [成绩排名]<=10
中国风 2009-03-15
  • 打赏
  • 举报
回复
select *,[成绩排名]=rank()over(order by 成绩 desc)
from table

select
[班级1]=sum(case when 班级=1 then 1 else 0 end),
[班级2]=sum(case when 班级=2 then 1 else 0 end),
[班级3]=sum(case when 班级=3 then 1 else 0 end),
................

from
(select *,[成绩排名]=rank()over(order by 成绩 desc) from table)t
where [成绩排名]<=10
rucypli 2009-03-15
  • 打赏
  • 举报
回复
1
select *,RANK() OVER (order by 成绩 desc) as 排名
from tb
dawugui 2009-03-15
  • 打赏
  • 举报
回复
1.某次考试(由考试ID决定)中,成绩排名(不同的学生分数一样时排名一样,如:1,2,2,4...)
--sql 2000
1)按班级排
SELECT * , 排名=(SELECT COUNT(成绩) FROM tb WHERE 班级 = t.班级 and 成绩 > t.成绩) + 1 FROM tb t ORDER BY t.班级 , 排名
2)按全部排
SELECT * , 排名=(SELECT COUNT(成绩) FROM tb WHERE 成绩 > t.成绩) + 1 FROM tb t ORDER BY t.班级 , 排名

--sql 2005
1)按班级排
SELECT * , 排名 = rank() over(partition 班级 order by 成绩 desc) FROM tb t ORDER BY t.班级 , 排名
2)按全部排
SELECT * , 排名 = rank() over(order by 成绩 desc) FROM tb t ORDER BY t.班级 , 排名

2.某次考试(由考试ID决定)中,全年级前十名在各班级的分布情况(即1班有多少个,2班有多少个...?)
1)--不考虑正好第十名有多个的情况
select 班级 , count(*) 数量 from
(
select top 10 * from tb order by 成绩 desc
) t
group by 班级
2)--考虑正好第十名有多个的情况
select 班级 , count(*) 数量 from tb where 成绩 in (select top 10 成绩 from tb order by 成绩 desc) group by 班级

3)--考虑某班可能不存在前十名的话,显示为0,仍然按上述两种情况考虑。
select m.班级 , isnull(n.数量 , 0) 数量 from
(
select distinct 班级 from tb
) m
left join
(
select 班级 , count(*) 数量 from
(
select top 10 * from tb order by 成绩 desc
) t
group by 班级
) n
on m.班级 = n.班级

select m.班级 , isnull(n.数量 , 0) 数量 from
(
select distinct 班级 from tb
) m
left join
(
select 班级 , count(*) 数量 from tb where 成绩 in (select top 10 成绩 from tb order by 成绩 desc) group by 班级
) n
on m.班级 = n.班级
dawugui 2009-03-15
  • 打赏
  • 举报
回复
[Quote=引用楼主 joehunter 的帖子:]
表结构如下:
考试ID|学生ID|班级|姓名|成绩
希望得出:
1.某次考试(由考试ID决定)中,成绩排名(不同的学生分数一样时排名一样,如:1,2,2,4...);
2.某次考试(由考试ID决定)中,全年级前十名在各班级的分布情况(即1班有多少个,2班有多少个...?)
多谢帮助,不甚感激!
[/Quote]

1.某次考试(由考试ID决定)中,成绩排名(不同的学生分数一样时排名一样,如:1,2,2,4...)
--sql 2000
1)按班级排
SELECT * , 排名=(SELECT COUNT(成绩) FROM tb WHERE 班级 = t.班级 and 成绩 > t.成绩) + 1 FROM tb t ORDER BY t.班级 , 排名
2)按全部排
SELECT * , 排名=(SELECT COUNT(成绩) FROM tb WHERE 成绩 > t.成绩) + 1 FROM tb t ORDER BY t.班级 , 排名

--sql 2005
1)按班级排
SELECT * , 排名 = rank() over(partition 班级 order by 成绩 desc) FROM tb t ORDER BY t.班级 , 排名
2)按全部排
SELECT * , 排名 = rank() over(order by 成绩 desc) FROM tb t ORDER BY t.班级 , 排名

2.某次考试(由考试ID决定)中,全年级前十名在各班级的分布情况(即1班有多少个,2班有多少个...?)
1)--不考虑正好第十名有多个的情况
select 班级 , count(*) 数量 from
(
select top 10 * from tb order by 成绩 desc
) t
group by 班级
2)--考虑正好第十名有多个的情况
select 班级 , count(*) 数量 from tb where 成绩 in (select top 10 成绩 from tb order by 成绩 desc) group by 班级
「已注销」 2009-03-15
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 roy_88 的回复:]
SQL codeselect *,[成绩排名]=rank()over(order by 成绩 desc)
from table

select
[班级1]=sum(case when 班级=1 then 1 else 0 end),
[班级2]=sum(case when 班级=2 then 1 else 0 end),
[班级3]=sum(case when 班级=4 then 1 else 0 end),
................

from
(select *,[成绩排名]=rank()over(order by 成绩 desc) from table)t
where [成绩排名]<=10
[/Quote]
claro 2009-03-15
  • 打赏
  • 举报
回复
帮顶。

34,597

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧