我試圖想出它得到每SQL用於獲取基於等級最高的用戶和使用效果,以獲得額外的數據
- 國名的SQL,
- 代碼,
- 數遊戲,
- 頂級球員的名字和
- 他的身份證
- 基於遊戲表中的SUM(等級)。
我很難根據評分獲得頂級玩家的名字。
我試圖想出它得到每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 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小提琴
您可以張貼在http://sqlfiddle.com –