私はすべてを取得するSQLを考え出そうとしています
- 国名、
- コード、
- ゲーム数、
- TOP選手の名前と
- 彼のID
- ゲーム テーブルの SUM(rating) に基づきます。
レーティングに基づいてトップ プレイヤーの名前を取得するのに問題があります。
私はすべてを取得するSQLを考え出そうとしています
レーティングに基づいてトップ プレイヤーの名前を取得するのに問題があります。
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
このクエリを試してください
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フィドルのデモです