过滤MS Access报告,以显示最近一次考试失败的学生加上之前的一次考试

时间:2015-11-13 19:59:06

标签: sql ms-access

我正在尝试使用相同的MS Access报告,但要将其过滤为仅显示符合特定条件的学生的姓名。不同的过滤器将分为标题报告,以便于访问。

我们学校将69或以下定义为考试成绩不合格。

如果最近一次考试的分数等于或低于69并且该学生在任何一次考试中得分等于或低于69,则该学生需要出现在风险报告中。

如果学生最近一次考试的分数等于或低于69且该学生在任何两门考试中的得分等于或低于69分,则需要出现在学术委员会报告中< /强>

我将如何编码?我会在查询中的过滤器设置或其他地方输入吗?

当前的数据库设计:

Exam Dates table: ExamCode, ExamDate

Exam Scores table: ExamCode, StudentID, Book, Version, Score

Students table: StudentID, StudentName

Exams query: ExamCode, ExamDate, StudentID, StudentName, Book, Version, Score

At Risk report: Header grouping current (most recent) exam date, columns showing StudentName, StudentID, and the Book, Version, and Score of the two exams that put them into the At Risk category. Sorted by book and then by StudentID.

Academic Board report: Same as above except showing three exams putting them into the Academic Board category.

1 个答案:

答案 0 :(得分:0)

您可以首先创建一个查询,选择上次考试不足的学生,并计算他们总共失败的次数,以及他们失败的第一次考试:

SELECT      StudentID,
            Count(*) AS FailCount,
            Max(ExamCode) AS LastExamCode,
            Min(ExamCode) AS FirstExamCode
FROM        [Exam Scores]
WHERE       Score <= 69
GROUP BY    StudentID
HAVING      Max(ExamCode) = (SELECT Max(ExamCode) FROM [Exam Dates])

将上述SQL保存为名称为 FailingStudents 的查询 然后,您可以从此查询中获取第一个报告的FailCount = 2和第二个报告的= 3

对于第一份报告,您还可以获得有关两门考试的开箱即用的代码。有了它,您可以通过再次为每个人加入[考试分数]来检索书籍,版本和分数。

对于第二个报告,它有点棘手:你有三个考试代码中的2个,但你可以通过要求其ExamCode介于(并且不等于)两个考试代码来加入[考试分数]极端。

以下是第一个报告的SQL,使用上面的查询:

SELECT      Students.StudentName,
            Students.StudentID,
            FirstFail.ExamCode,
            FirstFail.Book,
            FirstFail.Version,
            FirstFail.Score,
            LastFail.ExamCode,
            LastFail.Book,
            LastFail.Version,
            LastFail.Score
FROM        (((FailingStudents
INNER JOIN  Students
        ON  Students.StudentID = FailingStudents.StudentID)
INNER JOIN  [Exam Scores] LastFail
        ON  LastFail.ExamCode = FailingStudents.LastExamCode
        AND LastFail.StudentID = FailingStudents.StudentID)
INNER JOIN  [Exam Scores] FirstFail
        ON  FirstFail.ExamCode = FailingStudents.FirstExamCode
        AND FirstFail.StudentID = FailingStudents.StudentID)
WHERE       FailingStudents.FailCount = 2;

但请注意,对于您在评论中提供的数据,没有匹配的记录。

第二份报告可以基于之前SQL的扩展,如下所示:

SELECT      Students.StudentName,
            Students.StudentID,
            FirstFail.ExamCode,
            FirstFail.Book,
            FirstFail.Version,
            FirstFail.Score,
            SecondFail.ExamCode,
            SecondFail.Book,
            SecondFail.Version,
            SecondFail.Score,
            LastFail.ExamCode,
            LastFail.Book,
            LastFail.Version,
            LastFail.Score
FROM        ((((FailingStudents
INNER JOIN  Students
        ON  Students.StudentID = FailingStudents.StudentID)
INNER JOIN  [Exam Scores] LastFail
        ON  LastFail.ExamCode = FailingStudents.LastExamCode
        AND LastFail.StudentID = FailingStudents.StudentID)
INNER JOIN  [Exam Scores] FirstFail
        ON  FirstFail.ExamCode = FailingStudents.FirstExamCode
        AND FirstFail.StudentID = FailingStudents.StudentID)
INNER JOIN  [Exam Scores] SecondFail
        ON  SecondFail.ExamCode < FailingStudents.LastExamCode
        AND SecondFail.ExamCode > FailingStudents.FirstExamCode
        AND SecondFail.StudentID = FailingStudents.StudentID)
WHERE       FailingStudents.FailCount = 3;

注意最后的额外JOIN3。 此查询根据您提供的数据返回两个实例: 您可以在此fiddle中看到此内容,该内容基于MySQL,但原则相同。