基于排名返回Sql结果

时间:2016-06-15 15:32:31

标签: mysql sql

如何根据运动员在该运动中获胜的奖牌数量,使用以下6个表格返回给定国家和运动的前三名运动员名单?

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               |
+------------+----------------------------------+-------------------+

到目前为止我有这个代码,但这不会返回所需的输出?

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' ;

1 个答案:

答案 0 :(得分:1)

考虑运动员分组并计算奖牌。

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

如果您需要为每枚奖牌指定不同的点数,您可以将ORDER BY子句更改为:

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

或者,您可以创建一个表来保存值。