2016-06-15 20 views
1

特定の国とスポーツの上位3名の選手のリストを返すにはどうすればいいですか?ランクに基づくSql結果の返信

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

これはアスリート、カントリー、スポーツのGROUP BYとメダルのCOUNTを必要とするので、ORDER BYとMySQLのLIMITを使ってトップ3を得ることができます。 – argoc

+0

トップ3を獲得するには、ソートを追加する必要があります。しかし、どのように文字列である "メダル"を並べ替えることができますか?そのフィールドを整数にする方が良いです。 –

+0

ゴールド3、シルバー2、ブロンズ1、無し0のメダルとポイントのテーブルを追加することはどうですか?その&SUM(ポイント)の参加を追加し、idAthletesでグループ化し、ポイントの降順の合計3 – PaulF

答えて

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) 

+0

これは素晴らしい答えです!このコードから各選手の合計金額から各選手の得た合計メダルの合計を表示したい場合は、どうすればいいですか? – blueGOLD

+1

クエリにいくつかの列を追加しました。私はあなたが探していたものを見つけることを願っています。 – Shago

+1

これは私に多くの助けてくれてありがとう。 – blueGOLD

関連する問題