Change A MySQL-specific query will result in invalid values ββfor non-aggregate columns. Use a portable query instead.
Assuming you are working in MySQL (based on your question tag, as well as the original SQL query), you can make the following expression:
SELECT subID1, subID2,stu1,stu2,comparisonID,MAX(stu1vers+stu2vers) AS maxvers FROM comparisons WHERE assignmentID=9 AND stu1!=stu2 GROUP BY LEAST(stu1,stu2), GREATEST(stu1,stu2);
Strike>
If you need improved portability (also the ability to issue a request, for example, Postgres), you need a slightly more complex request using one JOIN here:
SELECT c1.subID1, c1.subID2,c1.stu1,c1.stu2,c1.comparisonID,c2.versmax FROM comparisons AS c1 INNER JOIN ( SELECT LEAST(stu1,stu2) AS stuA, GREATEST(stu1,stu2) AS stuB, MAX(stu1vers+stu2vers) AS versmax FROM comparisons WHERE assignmentID=9 AND stu1<>stu2 GROUP BY stuA, stuB ) AS c2 ON ((c1.stu1=c2.stuA AND c1.stu2=c2.stuB) OR (c1.stu2=c2.stuA AND c1.stu1=c2.stuB) ) AND c1.stu1vers+c1.stu2vers=c2.versmax WHERE c1.assignmentID=9 AND c1.stu1<>c1.stu2;
Note that a more portable query may still return two rows for a unique pair of students if both combinations give the same maxvers (unless you decide to provide a rule to distinguish between them), for example:
+--------+--------+------+------+--------------+---------+ | subID1 | subID2 | stu1 | stu2 | comparisonID | maxvers | +--------+--------+------+------+--------------+---------+ | 15 | 11 | 1 | 6 | 64 | 6 | | 11 | 3 | 6 | 1 | 55 | 6 | +--------+--------+------+------+--------------+---------+