有一个成绩表(Score)的结构如下:
====================================
Stu_ID     Course_ID    Score     ...001          1             50     ...
001          2             60     ...
001          3             70     ...
002          1             50     ...
002          2             90     ...
...          ...           ...    ...
=======================================
其中Stu_ID为学生号,Course_ID为课程号 ,Score 为成绩要求返回学生号,每个学生所修课程总数,每个学生所修课程的平均分,有多少门课程成绩<60分,有多少门课程成绩在60至80之间,有多少门课程成绩>=80分.即使以下这种格式:学生号  课程总数   平均分   <60分    60至80分     >=80分
001        3       60       1        2           0
002      ....

解决方案 »

  1.   

    select a.stu_id as 学号,avg(a.score) as 平均分,
    (select count(*) from student where stu_id=a.stu_id) as 课程总数,
    (select count(*) from student where stu_id=a.stu_id and score<60) as 低于六十分,
    (select count(*) from student where stu_id=a.stu_id and score>=80) as 高于八十分,
    (select count(*) from student where stu_id=a.stu_id and score between 60 and 79) as 六十到八十分
     from student as a group by stu_id;或许还有优化一点的方法
    懒得想了