为学生分配成绩并获得每个成绩

时间:2017-05-16 18:20:41

标签: sql oracle

我有以下select语句。我有学校,课程,学生和分数(1-100)。我想指定一个年级(A,B,C,D),然后获得每个年级的学生总数。我的结果只显示每个标记的计数,而不是等级的计数

select          
    schools.name as school_name,            
    courses.name as course,   
        CASE
             WHEN   ((studentgrades.averageScore / 50  * 100)  > 79)
                  THEN 'A' 
                  WHEN   ((studentgrades.averageScore / 50  * 100)  < 80) 
                         AND ((studentgrades.averageScore / 50  * 100)  >64)
                  THEN 'B' 
                  WHEN   ((studentgrades.averageScore / 50  * 100)  < 80) 
                         AND ((studentgrades.averageScore / 50  * 100)  >64)
                  THEN 'C' 
                  WHEN   ((studentgrades.averageScore / 50  * 100)  < 50)
                  THEN 'D'                         
          END as grade,             
    count(*)                 
from
    students,
    studentgrades,
    schools,
    courses
where
    studentgrades.studentid = students.studentid
    and studentgrades.schoolid = students.schoolid     
    and studentgrades.schoolid = schools.school_number 
    and courses.id = studentgrades.coursesid
    and studentgrades.averageScore is not null 
    and schools.name = 'St. Joe School'           
group by schools.name,  courses.name,standardsgrades.averageScore

我现在得到的是个别商标的数量(例如5名学生获得88%,3名获得85%等)

School Name     Course        Grade     Count        
St. Joe School  MATH 30         A         5
St. Joe School  MATH 30         A         3
St. Joe School  MATH 30         A         2
St. Joe School  MATH 30         A         1
St. Joe School  MATH 30         A         1
St. Joe School  MATH 30         A         2 
St. Joe School  MATH 30         A         3
St. Joe School  MATH 30         B         2
St. Joe School  MATH 30         B         5
St. Joe School  MATH 30         B         2
St. Joe School  MATH 30         B         1 
St. Joe School  MATH 30         B         2

我希望看到的是每个年级(A,B,C,D)的总数

School Name     Course        Grade     Count        
St. Joe School  MATH 30         A         30
St. Joe School  MATH 30         B         20
St. Joe School  MATH 30         C         10
St. Joe School  MATH 30         D         5

3 个答案:

答案 0 :(得分:1)

您需要按计算的成绩而不是平均成绩进行分组。您可以使用CTE,子查询或重复组子句中的整个表达式来执行此操作。

我发现基于CTE的解决方案位易于阅读:

with cte
as (
    select schools.name as school_name,
        courses.name as course,
        case when ((studentgrades.averageScore / 50 * 100) > 79) then 'A' when ((studentgrades.averageScore / 50 * 100) < 80)
                and ((studentgrades.averageScore / 50 * 100) > 64) then 'B' when ((studentgrades.averageScore / 50 * 100) < 80)
                and ((studentgrades.averageScore / 50 * 100) > 64) then 'C' when ((studentgrades.averageScore / 50 * 100) < 50) then 'D' end as grade
    from students
    join studentgrades on studentgrades.studentid = students.studentid
        and studentgrades.schoolid = students.schoolid
    join schools on studentgrades.schoolid = schools.school_number
        and studentgrades.schoolid = schools.school_number
    join courses on courses.id = studentgrades.coursesid
    where studentgrades.averageScore is not null
        and schools.name = 'St. Joe School'
    )
select school_name,
    course,
    grade,
    count(*)
from cte
group by school_name,
    course,
    grade;

此外,始终使用现代显式连接语法,而不是旧的基于逗号的连接。

答案 1 :(得分:0)

将您拥有的查询放入视图中,然后使用另一个GROUP BY和SUM查询它。

SELECT SchoolName, Course, Grade, SUM(Count)
FROM YourNewView
GROUP BY SchoolName, Course, Grade

答案 2 :(得分:0)

只需选择现有查询,如下所示:

My super title is fantastic