我正在尝试想出一个可以获取每个
- 国名,
- 代码,
- 游戏数量,
- TOP球员的名字和
- 他的身份证
- 基于游戏表中的 SUM(评级)。
我无法根据评分获得顶级球员的名字。
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 top 1 * from games inner join players on games.player_id=players.id inner join country on games.country=country.id order by rating desc
试试这个查询
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 fiddle 的演示