如何查看从参加考试第一次开始在所有考试中获得满分的学生人数?

时间:2014-11-02 19:32:30

标签: sql sql-server-2008-r2

我有以下数据库设计部分,我需要在第一次参加考试时找到在弹出考试中获得满分的学生列表。在详细信息中,我们假设我们有三名学生在不同的日期(或几天)内两次参加第一次弹出式考试(Exam101):

Student A: first time, he got 0
Student A: second time, he got 10

Student B: first time, he got 10

Student C: first time, he got 10
Student C: second time, he got 0

参加所有考试并从第一次参加考试获得满分的学生总数应为:2

我对数据库的设计是:

Students Table: StudentNo, Name, Year
Exams Table: ExamID, Title, ExamDate
StudentsExams Table: ID, ExamID, StudentNo, Score, CompletedON 

那么我可以找到这个结果?

2 个答案:

答案 0 :(得分:1)

如果满分始终为10(对于所有考试),您可以使用:

select s.studentno,
       s.name,
       s.year,
       se.examid,
       e.title,
       se.score,
       se.completedon
  from students s
  join studentsexams se
    on s.studentno = se.studentno
  join exams e
    on se.examid = e.examid
  join (select studentno, examid, min(completedon) as first_completed
          from studentsexams
         group by studentno, examid) v
    on se.examid = v.examid
   and se.studentno = v.studentno
   and se.completedon = v.first_completed
 where se.score = 10

如果构成"满分"根据考试的不同,我们需要知道如何确定每项考试的最高分数。根据您发布的内容,您的考试表似乎没有最高分数字段。

答案 1 :(得分:1)

with cte_num
as
(
select name,ExamID,count(CompletedON)
from Students s join StudentsExams se
on s.StudentNo=se.StudentNo
join Exams e
on e.ExamID=se.ExamID
where score=10
having count(CompletedON)=1)

select count * from cte_num