编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数
2012-09-01 18:38
363 查看
有以下两张表,
Class表
classid classname
1 高三(一)班
2 高三(二)班
3 高三(三)班
Student表
studentid studentName classid
1 张三 2
2 李四 1
3 王五 1
4 赵六 3
5 钱七 2
6 孙九 3
score表
scoreid course studentid score
1 数学 2 99
2 数学 3 60
3 数学 4 80
4 语文 5 79
5 语文 6 58
6 语文 1 66
7 英语 6 76
8 英语 4 87
9 英语 3 100
10 英语 2 69
编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数
SQL语句:
if exists(select count(*) from sysobjects where type='U' and name='#temp')
drop table #temp
select p.studentid,studentname,p.classid,classname,course,score into #temp from
( select studentid,studentname,student.classid,classname
from student right outer join class on student.classid=class.classid) as p
left outer join score on p.studentid=score.studentid
select (select top 1 studentname from #temp where classname=x.classname and course=x.course order by score desc) as '姓名',classname as '班级',course as '课程',max(score) as '分数'
from #temp x group by classname,course order by classname
出自:http://blog.csdn.net/one_money/article/details/5692120
Class表
classid classname
1 高三(一)班
2 高三(二)班
3 高三(三)班
Student表
studentid studentName classid
1 张三 2
2 李四 1
3 王五 1
4 赵六 3
5 钱七 2
6 孙九 3
score表
scoreid course studentid score
1 数学 2 99
2 数学 3 60
3 数学 4 80
4 语文 5 79
5 语文 6 58
6 语文 1 66
7 英语 6 76
8 英语 4 87
9 英语 3 100
10 英语 2 69
编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数
SQL语句:
if exists(select count(*) from sysobjects where type='U' and name='#temp')
drop table #temp
select p.studentid,studentname,p.classid,classname,course,score into #temp from
( select studentid,studentname,student.classid,classname
from student right outer join class on student.classid=class.classid) as p
left outer join score on p.studentid=score.studentid
select (select top 1 studentname from #temp where classname=x.classname and course=x.course order by score desc) as '姓名',classname as '班级',course as '课程',max(score) as '分数'
from #temp x group by classname,course order by classname
出自:http://blog.csdn.net/one_money/article/details/5692120
相关文章推荐
- 编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数
- 编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数
- sql查询每个学生的最高成绩mysql语句
- 查询每个学生的各科成绩sql语句
- sql语句如何按产品类别查询,取出每个类别的前三条记录
- 查询score中选学一门以上课程的同学中分数为非最高分成绩的记录
- SQL语句查询每个分组的前N条记录的实现方法
- SQL语句查询指定的数据,查询只选择了指定4门课程的学生
- 如何编写高效率的SQL查询语句
- 面试题:用一条sql语句查询出“每门”课程都大于80分的学生姓名
- 用一条SQL语句 查询出每门课程都大于80分的学生姓名" 的实现方式
- SQL查询语句-返回名称的分类
- SQL语句:查询出每个分组的前若干条记录
- sql查询语句,求每个分组中的最大值实例
- min聚合函数查询带有额外字段sql|dense_rank()over(partition)|+班级学生成绩最高
- 习题 9.5 有10个学生,每个学生的数据包括学号、姓名、3门课程的成绩,从键盘输入10个学生数据,要求输出3门课程总平均成绩,以及最高分的学生的数据(包括学号、姓名、3门课程成绩、平均分数)。
- 数据库表,3个字段,姓名,薪水,部门,用一条sql语句求每个部门薪水最高的人姓名
- 在oracle中查询已知表名的表中所有字段名,每个字段是否是主键,是否是外键,是否为空的sql语句
- SQL 数据库 学习 025 查询-08 模糊查询 --- 怎样编写模糊查询语句
- 查询数据库名表名字段名的SQL语句