I have a table comparisons. If I run
SELECT comparisonID,stu1Vers,stu2Vers,stu1,stu2
from comparisons
WHERE stu1!=stu2 and assignmentid=9;
I get something like:
+--------------+----------+----------+------+------+
| comparisonID | stu1Vers | stu2Vers | stu1 | stu2 |
+--------------+----------+----------+------+------+
| 287 | 12 | 2 | 1 | 6 |
| 286 | 12 | 1 | 1 | 6 |
| 276 | 11 | 2 | 1 | 6 |
| 275 | 11 | 1 | 1 | 6 |
| 266 | 10 | 2 | 1 | 6 |
| 265 | 10 | 1 | 1 | 6 |
| 257 | 9 | 2 | 1 | 6 |
| 256 | 9 | 1 | 1 | 6 |
...
| 391 | 19 | 1 | 1 | 6 |
| 392 | 19 | 2 | 1 | 6 |
+--------------+----------+----------+------+------+
I'd like to select the entire row where stu1Vers+stu2Vers is the maximum. I keep trying something along the lines of
select c.comparisonid,c.stu1vers,c.stu2vers,max(totvers)
from comparisons as c join
(select comparisonid, stu1vers+stu2vers as totvers
from comparisons where stu1!=stu2 group by comparisonid) as cm
on c.comparisonid = cm.comparisonid and c.stu1vers+c.stu2vers = cm.totvers;
but that returns a rather random assortment of things:
+--------------+----------+----------+--------------+
| comparisonid | stu1vers | stu2vers | max(totvers) |
+--------------+----------+----------+--------------+
| 220 | 1 | 1 | 21 |
+--------------+----------+----------+--------------+
I'm trying to get row 392 in the first table.