Question : SELECT TOP 10 COUNT(ID)....result top 10 list

Hi

I am trying to get top 10 of those who make most goals in our local soccer team...

I have a table named Goals:
GoalID(int)  PlayerID(int)  TeamID(int) columns

I have a players table:
PlayerID(int)   PlayerName(nvarchar)  IsActive(bit)  TeamID(int)  TeamCategoryID(int) columns

when i run this query:

SELECT TOP 10 COUNT(g.PlayerID) AS Antal,
(SELECT p.PlayerName FROM Players p WHERE g.PlayerID = p.PlayerID) AS Spelare,
(SELECT p.TeamCategoryID FROM Players p WHERE g.PlayerID = p.PlayerID) AS Serie
FROM Goals g
GROUP BY g.PlayerID
ORDER BY COUNT(g.PlayerID) DESC

i get columns
Antal (no of goals for the player)
Spelare (PlayerName)
Serie (TeamCategoryID)

It tops the player whom has made the most goals but i need to sort it by teamcategory as well... I need to make a list for each TeamCategoryID


Answer : SELECT TOP 10 COUNT(ID)....result top 10 list

well, let's see, I might have found the problem there...
1:
2:
3:
4:
5:
6:
7:
8:
9:
select *
  from ( SELECT count(g.PlayerID) antal, max(p.playerName) playerName, p.TeamCategory
              , row_number() over (partition by p.TeamCategory order by count(g.PlayerID) desc) r
         FROM Players p 
         LEFT JOIN Goals g
           ON g.playerID = p.PlayerID
         GROUP BY p.playerID, p.TeamCategory
 ) sq
WHERE sq.r <= 10
Open in New Window Select All
Random Solutions  
 
programming4us programming4us