2012-12-03 36 views

回答

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 
+0

結構和樣本數據非常感謝你,這是遠遠超出我的知識演示.. – Ando

+0

對不起額外的問題,但我想知道什麼要添加到SQL,以便我可以得到與該國的評分總和的列 – Ando

+0

添加此,'a.totalRating' –

0
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 
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

相關問題