[转载]编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数

转载自:https://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

编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数

2010年06月24日 17:24:00 one_money 阅读数:9041 标签: sqljoinclasstable更多

个人分类: SQL Server
 

有以下两张表,
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

[转载]编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数的更多相关文章

  1. delphi连接sql server数据库,并根据sql语句查询出数据显示--初级

    需要用到四个组件,分别为: 1.ADOConnection1 设置Connectionstring属性(连接串),loginPrompt属性控制是否连接记住了密码: 2.ADOQuery1 设置Con ...

  2. 后台异常 - sql语句查询出的结果与dao层返回的结果不一致

    问题描述 sql语句查询出的结果与dao层返回的结果不一致 问题原因 (1)select 中,查询的列名称重复,数据出现错乱 (2)使用不等号,不等号(!=,<>),查询出来的结果集不包含 ...

  3. 一个学生分数表,用sql语句查询出各班级的前三名

    昨天去一家公司面试,被这道题难住了,哎,又失去一次好的机会. 回来 之后就再想这个问题 表结构及数据如下:

  4. sql语句查询出的某字段内容截取

    select  LEFT(context,LENGTH(context)-1) context    from table; (效果: 1,2,3, 查询出: 1,2,3 )

  5. sql语句查询出数据重复,取唯一数据

    select distinct mr.id,ifnull(mr.pid,0) as pid,mr.name from sys_role_res srr left join main_res mr on ...

  6. sql语句查询出表里符合条件的第二条记录的方法

    创建用到的表的SQL CREATE TABLE [dbo].[emp_pay]( [employeeID] [int] NOT NULL, [base_pay] [money] NOT NULL, [ ...

  7. 关于如何用sql语句查询出连续的一串数字

    在数据库操作中,经常有一些这样的操作:插入诺干条测试数据.查询这个月的登录情况(没有登录的日期不能不存在,要显示数量为0),获取诺干条guid. 这些的基础都是怎么生成连续的一串数字  1 2  3  ...

  8. mysql 按类别之用一条SQL语句查询出每个班前10名学生数据

    select * from 学生信息表 a where 10 >  (select count(*) from 学生信息表 where 班级ID = a.班级ID and 班内名次 > a ...

  9. 转载用sql语句计算出mysql数据库的qps,tps,iops性能指标

    本帖最后由 LUK 于 2014-9-21 22:39 编辑 思路: 1 关注MYSQL三个方面的性能指标,分别为query数,transaction数,io请求数 2 在某个时间范围内(例如20秒) ...

随机推荐

  1. Vue.js-08:第八章 - 组件的基础知识

    一.前言 在之前的学习中,我们对于 Vue 的一些基础语法进行了简单的了解,通过之前的代码可以清晰的看出,我们在使用 Vue 的整个过程,最终都是在对 Vue 实例进行的一系列操作. 这里就会引出一个 ...

  2. asp.net core系列 52 Identity 其它关注点

    一.登录分析 在使用identity身份验证登录时,在login中调用的方法是: var result = await _signInManager.PasswordSignInAsync(Input ...

  3. entity cannot be tracked

    背景:EF Core项目中使用InMemory作为数据库提供程序,编写单元测试. 报错:“The instance of entity type 'Movie' cannot be tracked b ...

  4. 微服务(入门二):netcore通过consul注册服务

    基础准备 1.创建asp.net core Web 应用程序选择Api 2.appsettings.json 配置consul服务器地址,以及本机ip和端口号信息 { "Logging&qu ...

  5. Shiro详解

    Shiro Shiro集成Spring 加入Spring和Shiro的jar包 配置Spring及SpringMVC 参照:官方给出的案例shiro\samples\spring Shiro集成Web ...

  6. MongoDB之基本操作与日常维护

    MongoDB基本操作 MongoDB的基本操作主要是对数据库.集合.文档的操作,包括创建数据库.删除数据库.插入文档.更改文档.删除文档.和查询文档. 操作 描述 show dbs 查看当前实例下的 ...

  7. C++对象生存期&&static

    生存期,即从诞生到消失的时间段,在生存期内,对象的值或保持不变,知道改变他的值为止.对象生存期分为静态生存期和动态生存期两种. 静态生存期 指对象的生存期与程序运行期相同.在namespace中声明的 ...

  8. 设计模式系列1:单例模式(Singleton Pattern)

    定义 保证一个类仅有一个实例,并提供一个该实例的全局访问点.  --<设计模式GoF> UML类图 使用场景 当类只能有一个实例并且用户可以从一个众所周知的访问点访问它时. 创建一个对象需 ...

  9. Odoo:全球第一免费开源ERP权威性能测试报告完整版(绝对珍藏)

    Odoo平台简介 Odoo(以前叫OpenERP)是世界排名第一的开源ERP系统,最早由比利时一家公司开发,经过十几年发展,目前全世界Odoo的使用者超过2百万人,Odoo被翻译成几十种语言,Odoo ...

  10. windows凭据管理

    解决windows凭据无法保存的问题1: 运行-gpedit.msc(组策略)-计算机配置-管理模板-系统-凭据分配 双击右侧”允许分配保存的凭据用于仅NTLM服务器身份验证“ 在弹出的窗口中选中“已 ...