2

私はすべてを取得するSQLを考え出そうとしています

  • 国名、
  • コード、
  • ゲーム数、
  • TOP選手の名前と
  • 彼のID
  • ゲーム テーブルの SUM(rating) に基づきます。

レーティングに基づいてトップ プレイヤーの名前を取得するのに問題があります。

ここに画像の説明を入力

SQLFiddle デモ

4

3 に答える 3

1
SELECT  x.Country AS CountryName,
        x.Code,
        a.totalCount as NumberOfGames,
        y.Name AS PlayersName,
        y.ID AS PlayersID,
        a.totalRating
FROM    (
            SELECT  player_ID, Country, COUNT(*) totalCount, SUM(Rating) totalRating
            FROM    Games
            GROUP BY player_ID, Country
        ) a 
        INNER JOIN
        (
            SELECT Country, Max(totaLRating) maxRating
            FROM
            (
                SELECT  player_ID, Country, SUM(Rating) totalRating
                FROM    Games
                GROUP BY player_ID, Country
            ) s
            GROUP BY Country
        ) b ON a.Country = b.Country AND
                a.totalRating = b.maxRating
        INNER JOIN Country x
            ON a.Country = x.ID
        INNER JOIN Players y
            ON a.player_ID = y.ID
于 2012-12-03T07:16:02.980 に答える
0

このクエリを試してください

select 
    c.id as country_id,
    c.name as Country,
    c.code as CCode,
    g.T_Games,
    p.id as Player_id,
    p.name as Player
from country as c
left join (select country , count(country) as T_Games from games group by country) as g on g.country = c.id
left join (select id , country , player_id , max(rating) from games group by country) as gl on gl.country = c.id
left join (select id , name from player) as p on p.id = gl.player_id

これがSQLフィドルのデモです

http://sqlfiddle.com/#!2/f74a7/1

于 2012-12-03T07:23:16.480 に答える