Question

I have a table of student scores. Students can take the test multiple times, we only care about the highest score.

So the table looks something like this:

UserID      CatID       Score
20          5           60
20          5           85
20          5           80
20          6           90
20          7           80
20          7           75
20          7           90
22          5           75
22          5           85

Want to get back one row based on a UserID variable that looks like this:

UserID      Cat5        Cat6        Cat7
20              85          90          90

Here's what I'm using so far as a query:

SELECT score AS Score
, catid
FROM `quiz_result` 
WHERE userid=65 and catid=5
ORDER BY score DESC
LIMIT 0, 1

Not sure how to get the other categories in one row...thoughts?

Thanks!

Was it helpful?

Solution

Try this piece:

SELECT catID, MAX( score ) FROM `quiz_result`
GROUP BY catID;

The output isn't exactly same as in your sample, but it pretty much gives what you need.

EDIT

Output will be like:

catID      MAX(score)
Cat5        85
Cat6        90
Cat7        90

To get other columns, just include their names in the SELECT query.

OTHER TIPS

Try this :

SELECT CONCAT_WS(',', catID) AS catID, CONCAT_WS(',', MAX(score)) FROM `quiz_result` GROUP BY catID;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top