I have a table like this;

event_id  pair  versus  score
       1     1      11    100
       1     1      12      0
       1    11       1      0
       1    12       1     50

Now I want to get the both scores for a given pair on the same result row. Like;

event_id pair versus score1 score2
       1    1     11    100      0
       1    1     12      0     50

Any ideas?


Assuming you always have pair , the following should give you desired result:

SELECT a.event_id ,a.pair,a.versus,
a.score as score1, b.score as score2
FROM table_name a
INNER JOIN table_name b on
(a.event_id = b.event_id and b.pair =a.versus and b.versus=a.pair and a.pair<b.pair)

Side notes.
1. It makes sense to have a surrogate primary key column in the table.
2. The query would look much cleared if you had one more column which serves as pair identifier (ie. both 1-11 and 11-1 would share the same pair_identifier).
3. a.pair<b.pair added to avoid duplicates in resultset

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.