SQL-评估多行中的最小值,然后过滤以显示唯一行列表吗?

时间:2019-06-03 18:56:30

标签: sql oracle oracle-sqldeveloper

我的最终目标是创建一个荣誉榜学生名单。每个学生有多行,每个年级一个。我想说的是,看看他们在这些行中的成绩;如果他们的年级都不低于80%,则仅显示1个学生姓名。

我只是从这个开始,但是我被困住了,我不知道如何评估多行作为选择唯一列表的标准。

SELECT students.first_name, students.last_name, storedgrades.storecode, storedgrades.percent,storedgrades.course_name
FROM storedgrades join
     students
     on students.ID = storedgrades.StudentID
where students.enroll_status=0  AND
      storedgrades.termid>2799  AND
      storedgrades.storecode = 'Q4'

成绩表示例:

BOB  A  95
BOB D  65
ANDREA  B  85
ANDREA  A  95

示例结果:

ANDREA

2 个答案:

答案 0 :(得分:0)

使用聚合。我想这就是你想要的:

select s.id, s.first_name, s.last_name
from students s join
     storedgrades sg 
     on s.ID = sg.StudentID
where s.enroll_status = 0  and
      sg.termid > 2799  AND
      sg.storecode = 'Q4'
group by s.id, s.first_name, s.last_name
having count(*) = 5 and  -- you want all five courses
       min(scorecode) >= 80;

答案 1 :(得分:0)

选择一个不同的学生姓名列表,然后将“不存在”与成绩低于80%的成绩表进行比较

SELECT distinct students.ID, students.first_name, students.last_name
FROM storedgrades s join
     students
     on students.ID = storedgrades.StudentID
where students.enroll_status=0  AND
      storedgrades.termid>2799  AND
      storedgrades.storecode = 'Q4' and not exists
      (select 'x' from storedgrades s2 where s2.students.StudentID = s.StudentID and s2.first_name = s.first_name and 
s.last_name = s2.last_name and s2.percent < 80)

编辑:添加了StudentID到联接中