Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query Selecting rows where data doesn't change

Tags:

sql

ms-access

I have a table of golf match-ups taken from Betfair's downloadable CSV files of historical data. They look like this:

event         selection
S. Garcia     Garcia
S. Garcia     Woods
P. Mickelson  Mickelson
P. Mickelson  Donald
E. Els        McIlroy
E. Els        Els

I need a query that provides the following output:

event         selection_a  selection_b
S. Garcia     Garcia       Woods
S. Garcia     Woods        Garcia
P. Mickelson  Mickelson    Donald
P. Mickelson  Donald       Mickelson
E. Els        McIlroy      Els
E. Els        Els          McIlroy

To conclude, the event name is just one of the players in the match-up. Within each match up there will be two players, how do I write a query that recognises that Garcia played Woods, Mickelson played Donald and Els played McIlroy?

Help! :-)

like image 834
RichieCunningham Avatar asked Feb 21 '23 01:02

RichieCunningham


1 Answers

SELECT
    g.event,
    Min(g.selection) AS selection_a,
    Max(g.selection) AS selection_b
FROM Golf_matches AS g
GROUP BY g.event
UNION ALL
SELECT
    g.event,
    Max(g.selection),
    Min(g.selection)
FROM Golf_matches AS g
GROUP BY g.event
ORDER BY 1, 2;
like image 147
HansUp Avatar answered Feb 22 '23 15:02

HansUp