Xvid Xvid - 5 months ago 16
SQL Question

Returning Sql Results based on Ranking

How do I return a list the top three athletes for a given Country and Sport Using the 6 tables below based on the number of medals the athletes win in that Sport ?

ATHLETES

+------------+-----------------+---------------+-------------------------+
| idATHLETES | ATHLETENAME | TEAMS_idTEAMS | TEAMS_COUNTRY_idCOUNTRY |
+------------+-----------------+---------------+-------------------------+
| JG | JUSTIN GATLIN | USA-TF-MEN | USA |
| MS | MARIA SHARAPOVA | RUS-WTA | RUS |
| SW | SERENA WILLIAMS | USA-WTA | USA |
| UB | USAIN BOLT | JAM-TF-MEN | JAM |
| VW | VENUS WILLIAMS | USA-WTA | USA |
+------------+-----------------+---------------+-------------------------+

EVENTS

+------------+---------------+---------------------+------------------------+----------------------------------+--------------------------+
| idEVENTS | EVENTNAME | ATHLETES_idATHLETES | ATHLETES_TEAMS_idTEAMS | ATHLETES_TEAMS_COUNTRY_idCOUNTRY | VARIOUS_SPORTS_SPORTS_ID |
+------------+---------------+---------------------+------------------------+----------------------------------+--------------------------+
| ATH | ATHLETICS | JG | USA-TF-MEN | USA | TRACK-AND-FIELD |
| ATH | ATHLETICS | UB | JAM-TF-MEN | JAM | TRACK-AND-FIELD |
| TEN | TENNIS | MS | RUS-WTA | RUS | WOMENS_TENNIS |
| TEN | TENNIS | VW | USA-WTA | USA | WOMENS_TENNIS |
| TEN-DOUBLE | TENNIS DOUBLE | SW | USA-WTA | USA | WOMENS_TENNIS |
| TEN-DOUBLE | TENNIS DOUBLE | VW | USA-WTA | USA | WOMENS_TENNIS |
+------------+---------------+---------------------+------------------------+----------------------------------+--------------------------+

RESULTS

+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+----------------------------------------
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+---------------------------------+
| idRESULTS | STATUS | MEDALS | EVENTS_idEVENTS | EVENTS_ATHLETES_idATHLETES | EVENTS_ATHLETES_TEAMS_idTEAMS | EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY | EVENTS_VARIOUS_SPORTS_SPORTS_ID |
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+---------------------------------+
| results1 | DID-NOT-WIN | SILVER | TEN | MS | RUS-WTA | RUS | WOMENS_TENNIS |
| results1 | WON | GOLD | TEN | VW | USA-WTA | USA | WOMENS_TENNIS |
| results2 | DID-NOT-WIN | BRONZE | ATH | JG | USA-TF-MEN | USA | TRACK-AND-FIELD |
| results2 | WON | GOLD | ATH | UB | JAM-TF-MEN | JAM | TRACK-AND-FIELD |
| results3 | WON | GOLD | TEN-DOUBLE | SW | USA-WTA | USA | WOMENS_TENNIS |
| results3 | WON | GOLD | TEN-DOUBLE | VW | USA-WTA | USA | WOMENS_TENNIS |
+-----------+-------------+--------+-----------------+----------------------------+-------------------------------+-----------------------------------------+---------------------------------+

VARIOUS_SPORTS

+-----------------+----------------------+
| SPORTS_ID | SPORTS_NAME |
+-----------------+----------------------+
| TRACK-AND-FIELD | MENS TRACK AND FIELD |
| WOMENS_TENNIS | WOMENS TENNIS |
+-----------------+----------------------+

THE_COUNTRY

+-----------+-------------+
| idCOUNTRY | COUNTRYNAME |
+-----------+-------------+
| JAM | JAMAICA |
| RUS | RUSSIA |
| USA | USA |
+-----------+-------------+

THE_TEAMS

+------------+----------------------------------+-------------------+
| idTEAMS | TEAMNAME | COUNTRY_idCOUNTRY |
+------------+----------------------------------+-------------------+
| JAM-TF-MEN | jamaican track and field men | JAM |
| RUS-WTA | russian women tennis association | RUS |
| USA-TF-MEN | usa track and field men | USA |
| USA-WTA | usa womens tennis association | USA |
+------------+----------------------------------+-------------------+


I have this code so far but this does not return the required output ?

SELECT idATHLETES, ATHLETENAME, TEAMS_COUNTRY_idCOUNTRY, COUNTRYNAME FROM
athletes
JOIN EVENTS ON idATHLETES = idEVENTS
JOIN teams ON TEAMS_idTEAMS =idTEAMS
JOIN country ON COUNTRY_idCOUNTRY = idCOUNTRY
JOIN RESULTS ON ATHLETES_idATHLETES = EVENTS_ATHLETES_idATHLETES

WHERE EVENTS_VARIOUS_SPORTS_SPORTS_ID = 'WOMENS_TENNIS' AND EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY ='USA' ;

Answer

Consider grouping by athlete and counting the medals.

SELECT idATHLETES, ATHLETENAME, TEAMNAME, COUNTRYNAME
      , MEDALS = SUM(CASE WHEN MEDALS IS NULL THEN 0 ELSE 1 END)
      , SCORE = SUM(CASE MEDALS WHEN 'Gold' THEN 3
                WHEN 'Silver' THEN 2
                WHEN 'Bronze' THEN 1
                ELSE 0 END)
      , GOLD = SUM(CASE MEDALS WHEN 'Gold' THEN 1 ELSE 0 END)
      , SILVER = SUM(CASE MEDALS WHEN 'Silver' THEN 1 ELSE 0 END)
      , BRONZE = SUM(CASE MEDALS WHEN 'Bronze' THEN 1 ELSE 0 END)
FROM ATHLETES
JOIN TEAMS ON TEAMS_idTEAMS = idTEAMS
JOIN COUNTRY ON COUNTRY_idCOUNTRY = idCOUNTRY
JOIN RESULTS ON ATHLETES_idATHLETES = EVENTS_ATHLETES_idATHLETES
WHERE EVENTS_VARIOUS_SPORTS_SPORTS_ID = 'WOMENS_TENNIS'
AND EVENTS_ATHLETES_TEAMS_COUNTRY_idCOUNTRY ='USA'
GROUP BY idATHLETES, ATHLETENAME, TEAMNAME, COUNTRYNAME
ORDER BY SUM(CASE WHEN MEDALS IS NULL THEN 0 ELSE 1 END) DESC
LIMIT 3

If you need to assign different point to each medal you could do so by changing the ORDER BYclause to:

ORDER BY SUM(CASE MEDALS WHEN 'Gold' THEN 3
                WHEN 'Silver' THEN 2
                WHEN 'Bronze' THEN 1
                ELSE 0 END)

Alternatively you could create a table to hold the values.