zoukankan      html  css  js  c++  java
  • [转载]编写SQL语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数

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

    有以下两张表,
    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语句查询出每个各科班分数最高的同学的名字,班级名称,课程名称,分数

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

  • 相关阅读:
    python字典
    python元组
    python list列表
    deepin常用操作
    Be a child.Be a man...
    “夹心阶层”——面对高昂的房价,要面对现实,广州,除了劳动力便宜,什么都贵。
    男人一生中16件事情(转载)
    被老板臭骂了一顿,但受益匪浅。
    陪伴
    高品质生活
  • 原文地址:https://www.cnblogs.com/yuchao-chen/p/9817141.html
Copyright © 2011-2022 走看看