-1

I have students that take the ACT test multiple times a year, but I only want the last ACT score they have taken.

My table has StudentID, ACTDate (YYYYMM format only), and ACTScore.

How can I get the last/latest score for every student?

I've looked at Unique, First, Limit... - all do not do what I want! I'm a very new to SQL (but not programming), please be gentle! :)

I'm using SQuirrel SQL 3.7 - MySQL if that matters!

Pat
  • 1
  • given that ids are assigned in an incremental manner, you coud just select the max id from actscore and that should give you the last row inserted in that table. If you want to be more formal, store the date of the score in your table. – Prefijo Sustantivo Apr 11 '16 at 20:27
  • http://dev.mysql.com/doc/refman/5.7/en/example-maximum-column-group-row.html – JamieD77 Apr 11 '16 at 20:29

3 Answers3

0
select a.StudentID, a.ACTScore, a.ACTDate
from mytable a
where a.ACTDate = (select max(b.ACTDate)
                     from mytable b
                    where a.StudentID = b.StudentID);
T Gray
  • 712
  • 4
  • 10
0

This should answer your question. You would just need to change it out for your ACT scores

Community
  • 1
  • 1
jumper
  • 74
  • 1
  • 9
0

In MSSQL or Oracle I would do:

    SELECT 
        StudentID, 
        MAX(ACTDate) AS LAST_DATE, 
        MAX(ACTScore) AS Score
    FROM Students
    GROUP BY StudentID

So we just group the students by their ID and we use an aggregate function MAX over the date that will show us the latest date, then we use another aggregate function MAX over the ACTScore in order for us to include it in the selection.

I guess that something similar should be done in MySQL. Let me know if it (not)worked.

Zdravko Donev
  • 402
  • 5
  • 22