!!!有一张成绩表(姓名)(班级)(成绩)三个字段,要求写个sql语句选择出不同班级的成绩前三名的学生的信息

Dicky81 2006-02-24 10:12:33
有一张成绩表(姓名)(班级)(成绩)三个字段,要求写个sql语句选择出不同班级的成绩前三名的学生的信息
...全文
2150 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wumingxiaodi 2006-02-25
  • 打赏
  • 举报
回复
select a.* from 成绩表 a
where not exists(select 1 from 成绩表 b where a.班级=b.班级 and b.成绩>a.成绩
group by b.班级 having count(1)>=3)
order by 班级,成绩 desc

已测试通过,注意如果同一班有多人成绩相同且成绩值在前三名内则这些人都会包含在结果集中.
haonanernet 2006-02-25
  • 打赏
  • 举报
回复
(1)select 姓名,班级,成绩 from 表 a where (select COUNT(1) FROM 表 where 班级=a.班级 and 姓名>a.姓名)<=2
(2)select 姓名,班级,成绩 from 表 a where 姓名 in(select top 3 姓名 from 表 where 班级=a.班级 order by 成绩 desc)
(3)select 姓名,班级,成绩 from 表 a where not exsits(select 1 from 表 where 班级=a.班级 and 成绩>a.成绩 HAVING count(*)>=3)
hbzy123 2006-02-24
  • 打赏
  • 举报
回复
select count(*) from uds_class where classparentid in (select * from #classid)

drop table #classid
最后一句写错了
应该是
select * from uds_class where classparentid in (select * from #classid)
,
具体内容你换成你自己的吧
hbzy123 2006-02-24
  • 打赏
  • 举报
回复
create table uds_Class
(
classid int, --学生ID
classparentid int, --班级ID
mark int --分数
)

declare @classid varchar(1000)
create table #classid(ids int)

set @classid=''
select @classid=@classid+convert(varchar,a.classparentid)+','
from(select distinct classparentid from uds_Class where classparentid<>-1) a

select @classid
select substring(@classid,charindex(',',@classid)+1,len(@classid))
declare @tempid int

while len(@classid)>0
begin
set @tempid =convert(int,substring(@classid,1,charindex(',',@classid)-1))
set @classid=substring(@classid,charindex(',',@classid)+1,len(@classid))
insert into #classid
select top 3 classid
from uds_Class
where ClassParentID=@tempid
end
select count(*) from uds_class where classparentid in (select * from #classid)

drop table #classid
singlepine 2006-02-24
  • 打赏
  • 举报
回复
select top 3 姓名,班级,成绩 from tablename group by 姓名,班级,成绩 order by 成绩 desc

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

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