0

I am trying to use the same MS Access report but have it be filtered to only present names of students matching certain criteria. The different filters will be made into separately titled reports for easier access.

Our school defines 69 or below as a failing exam score.

If the score for the most recent exam is at or below 69 AND the student has scored at or below 69 on any ONE previous exam, the student needs to show up in the At Risk Report.

If the score for the student's most recent exam is at or below 69 AND the student has scored at or below 69 on any TWO previous exams, they need to show up in the Academic Board Report.

How would I code this? Would I enter it on the filter settings in my query, or somewhere else?

Current database design:

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.
Paul Williams
  • 1,554
  • 7
  • 40
  • 75
  • "The most recent exam" is that the same single exam for everyone, or is that dependent on the student, in the sense that the last exam a student has actually participated in is not necessarily the same one for everyone? – trincot Nov 13 '15 at 20:17
  • What did you try so far? – trincot Nov 13 '15 at 20:42
  • They all take their exams at the same time. Really not sure how to approach this... I know it would start with coding for a filter that shows all students with >69 for the maximum ExamCode (this field name is based off of a numeric date value with an E in front of it, ie "E20151021" to represent the exam taken on October 21, 2015) then finding all previous >69 scores and counting them. I'm lost at this point. – StudentSuccess1 Nov 13 '15 at 20:44
  • Do you need the exam, book, version and score of the two insufficient exams in 2 rows with same student information or as 2 columns for exam, 2 for book, 2 for version and 2 for score on 1 row? Secondly, do you need those that appear in the second report to also appear in the first? Do students who had insufficient on 4 exams still to apear in any of the reports? – trincot Nov 13 '15 at 21:11
  • 1. I would like to keep each student's data on one row. Exam date, Book, Version, Score -- then the same data for the the next exams. The right most exam would be the "current" (most recent) exam, then going backward in time from right to left. Then at the far left is the student identifying info. – StudentSuccess1 Nov 13 '15 at 21:16
  • 2. The students who are At Risk are different from those who are are Academic Board since the two groups consist of students who have failed a different quantity of exams as of this point in time. As we progress through our next exam dates the identities of those in each group will change as some fail more exams while some stay with the same number of failures if they pass the most recent exam. – StudentSuccess1 Nov 13 '15 at 21:19

1 Answers1

0

You could start by creating a query that selects the students that had insufficient for the last exam, with a count on how many they failed in total, and which was their first exam that they failed:

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])

Save the above SQL as a query with name FailingStudents From this query you could then take the records with FailCount = 2 for the first report, and = 3 for the second.

For the first report you will also have out-of-the-box the codes of the two exams concerned. With that you can retrieve the Book, Version and Score by joining [Exam Scores] again for each of them.

For the second report it is bit more tricky: you have 2 out of the three exam codes, but you can join [Exam Scores] for the middle one by requiring that its ExamCode is between (and not equal to) the two extremes.

Here is an SQL for the first report, using the query made above:

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;

Note however, that with the data you provided in comments, there are no matching records.

The second report could be based on an extension of the previous SQL, like this:

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;

Note the extra JOIN and the 3 at the end. This query returns two instances based on the data you provided: You can see this in this fiddle, which is based on MySQL, but it is the same principle.

trincot
  • 317,000
  • 35
  • 244
  • 286
  • It is giving me an error message when I go to my query in SQL View and try to input the code from here. It says the statement with Max includes a reserved word or an argument name that is misspelled or missing. – StudentSuccess1 Nov 13 '15 at 21:56
  • Does the sub-select at the end run by itself? – trincot Nov 13 '15 at 22:02
  • I don't know what that means. I deleted the Max and Min statements and now it is giving an error saying the syntax is off on the Select Student ID statement. – StudentSuccess1 Nov 13 '15 at 22:09
  • Run: `SELECT Max(ExamCode) FROM [Exam Dates]` Does that give errors? Are the table names and column names you have specified in the question exact? I missed some commas to separate the columns in my answer. Check the correction. – trincot Nov 13 '15 at 22:11
  • It produces a valid value. It is E2015AB. This is for students who were already considered AB (Academic Board) at the time this data was given to me. I think I should move these students into the numbered ExamCodes that exist and abolish the E2015AB code. – StudentSuccess1 Nov 13 '15 at 22:23
  • OK, I now better understand. Yes, you should not have these special exam codes. Did you try the bigger query with the fix on the commas? – trincot Nov 13 '15 at 22:26
  • I changed the E2015AB to be the most recent exam date (since they had become AB as of the last exam). I tried the query again with all commas removed. All field names are exact. It is now giving me a syntax error for StudentID Count, saying there is a missing operator. – StudentSuccess1 Nov 13 '15 at 22:32
  • Comma's removed? Why? You need to have the comma's like I put them in my answer. – trincot Nov 13 '15 at 22:37
  • I put in the code you gave me. Now it is running the query with the fields you created showing up in Datasheet view. Is it normal for there to be no data displaying? I do not see any data showing up in the records on this query. – StudentSuccess1 Nov 13 '15 at 22:41
  • Without the actual data, I cannot know this. You should find out yourself: check the max ExamCode again, then check if you have records in [Exam Scores] of less than or equal 69 for that particular ExamCode. If you got some, note down the StudentIDs and see if you can find other exams where they have a low score. If you can specify some relevant data in your original question, enough to produce some results, that could be useful. – trincot Nov 13 '15 at 22:46
  • Ugh, more problems on my end due to input of data. Thanks for these reminders. I had some score data left blank because the input for them had been "A" for absent. I will need to enter these as zeros. Will do so and try again. – StudentSuccess1 Nov 13 '15 at 22:51
  • E20150831 126148 5 B 96 E20150915 126148 6 B 54 E20151021 126148 6 A 42 E20151105 126148 7 B 52 E20150831 126199 4 A 60 E20150915 126199 4 C 32 E20151021 126199 4 A 46 E20151105 126199 4 B 20 E20150831 126214 3 A 74 E20150915 126214 4 C 50 E20151021 126214 4 A 42 E20151105 126214 4 B 42 E20150831 126293 4 A 66 E20150915 126293 4 C 44 E20151021 126293 4 A 40 E20151105 126293 4 B 40 E20150831 126302 5 B 84 E20150915 126302 6 B 70 E20151021 126302 7 A 92 E20151105 126302 8 A 62 – StudentSuccess1 Nov 13 '15 at 22:57
  • I cannot get the above to display as a table or even with linebreaks. In any event, it is the examcode followed by the student ID number followed by the book number then the book version then the score. – StudentSuccess1 Nov 13 '15 at 23:03
  • In this data there are no students with exactly 2 scores on or below 69. There are students with 1, 3 or 4 exams for which they have such scores, but not with 2. So your first report would be empty. – trincot Nov 13 '15 at 23:31
  • I have added to my answer with enough material to create the two reports, I think. Good luck! – trincot Nov 13 '15 at 23:43
  • OK, no problem, there will be in the larger amount body of data (I think). How do I enter SQL into a report? I see an option on the report design view to "View Code" and then it comes up with a screen that says "Option Compare Database"... do I post your code under that, then save and return to report? – StudentSuccess1 Nov 13 '15 at 23:45
  • No, first create a query with the first `SQL` I provided, and call it **FailingStudents**. Then create 2 more queries for the other 2 `SQL` statements, and give them appropriate names. Then create a report based on one of these latest queries. You can use the report wizard or select the fields from the fields page. – trincot Nov 13 '15 at 23:51
  • I get an error on this code: Students.StudentID = FailingStudents.StudentID INNER JOIN [Exam Scores] LastFail ON LastFail.ExamCode = FailingStudents.LastExamCode – StudentSuccess1 Nov 14 '15 at 00:05
  • What is the error? Did you create the query with the name **FailingStudents**? – trincot Nov 14 '15 at 00:08
  • Yes, I created that query. In the next query, it is saying there is a missing operator in the expression I referenced. – StudentSuccess1 Nov 14 '15 at 00:13
  • It might be [this problem](http://stackoverflow.com/questions/7854969/sql-multiple-join-statement). I added the additional brackets in my answer. You should try again with that. – trincot Nov 14 '15 at 00:19
  • I did. Weird... now it is just saying "Syntax error in JOIN operation". It is selecting the word ON right below the first INNER JOIN. – StudentSuccess1 Nov 14 '15 at 00:29
  • I had the opening brackets misplaced. Try again. BTW, you should try a bit harder to solve issues yourself, though. I don't have MS Access at hand, so it is a bit harder for me to get it right. – trincot Nov 14 '15 at 00:46
  • Is a good way to solve issues myself to search for errors here on StackOverflow? Both have created reports but no data is showing. I think me entering the 0 scores has done something. I will search for these issues myself. I appreciate all of your help! It is great. – StudentSuccess1 Nov 14 '15 at 00:54
  • Make sure you have identified manually in the data at least one student that should show up in the report. Otherwise you cannot be sure if the report is wrong or right. – trincot Nov 14 '15 at 00:58
  • OK, will do. Much appreciated! – StudentSuccess1 Nov 14 '15 at 01:00
  • I put in dummy data and it is still not displaying anything in the At-Risk Report. Have not tried the Academic Board Report yet. I wonder if it has something to do with the ID (primary key) being present in a text book in the report group header. That is how it is on my other reports. – StudentSuccess1 Nov 14 '15 at 18:15
  • First check that the queries return the expected data, before checking the report. Do you really want to have group headers by text book? – trincot Nov 14 '15 at 18:22
  • I misspoke. I don't want grouping in these reports, just sorting, so I will fix that. The query itself is not returning any data either. – StudentSuccess1 Nov 14 '15 at 18:30