Jump to content

[SOLVED] Can this be done without using subqueries?


artacus

Recommended Posts

I have a table that has a student's reading fluency scores. Students will be tested multiple times thruout the year and with no set schedule. (One student may be tested 2x a year, another weekly)

So I need to get the most recent score for each student. I've been doing it using sub-queries (get max(test_date) for each student). But now I have a case where I need to do it w/o using subqueries.

[code]
SELECT studentID, @mtd:=MAX(test_date), MAX(IF(test_date = @mtd, score,0)) AS score,
FROM reading_fluency AS rf
WHERE test_date > '2006-09-01'
GROUP BY studentID
[/code]
Doesn't work because @mtd ends up being set as the MAX of all students, not the student we are currently working with. BTW MAX(test_date) when run w/o assigning it to a variable DOES select the max for that student.

[code]
SELECT studentID, SUM(IF(test_date = MAX(test_date), score,0)) AS score,
FROM reading_fluency AS rf
WHERE test_date > '2006-09-01'
GROUP BY studentID
[/code]
Doesn't work, I get "invalid use of group function" ... the MAX() inside the SUM()

Can it be done?
Link to comment
Share on other sites

Hmm.. I can offer a low-tech solution.  Select all the test results and [code=php:0]ORDER BY studentID, test_date DESC[/code].  Then use a foreach loop to pick the first entry for each student.  You can also order it ASC and pick the last entry (that can be convenient, since you just keep assigning.. and the most recent assignment for that student is their latest test score).

I'm very surprised that the max ends up being for all students.  I guess it's a side-effect of how assignment is implemented.

Anyway, my answer to your original question is "No, I can't think of how to do this without subqueries".  It's breaking the rules to aggregate one column and then use that result during the aggregation of another column.
Link to comment
Share on other sites

Well the problem is that I have a bunch of assessments (about  12 test groups, 100 tests & 800 test strands) and I am trying to map these so I can find out where each piece of information is located in the database and then generate statistics and averages for the district, schools and classes.

Most of the tests occur at a given time (1st, 2nd, 3rd trimester) so its easy to pull that information. But because reading fluency is given year round, I'm having a hard time mapping it. If there were a work around to get @mtd to work, it would have fit in with all of my other assessments but  now I'll have to calculate the stats w/o mapping them.
Link to comment
Share on other sites

What is the reason that you can't use a temporary table?  What fenway means is something like this (my syntax may not be correct for mysql):

[code=php:0]CREATE TEMP TABLE student_max_test_date AS SELECT studentID, MAX(test_date) AS test_date
FROM reading_fluency
WHERE test_date > '2006-09-01'
GROUP BY studentID;[/code]

[code=php:0]SELECT studentID, test_date, score
FROM reading_fluency
JOIN student_max_test_date USING (studentID, test_date)[/code]


There's no need for the awkward construct for score here, as the max test_date has already been found.  Then a simple join gets you the scores that match those max test_dates.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.