I have two tables Players(Name,Surname,Goals,TeamID) and Teams(Name,TeamID).
In the following query I get the average goals' number per team and select the players who have a number of goals greater than the average.
SELECT Players.Name, Players.Surname, Teams.Name, Players.Goals, (SELECT ROUND(AVG(Players.Goals)) FROM Players GROUP BY Players.TeamID) AS TeamAverage
FROM Players
JOIN Teams ON Players.TeamID = Teams.TeamID
WHERE Players.Goals > (SELECT ROUND(AVG(Players.Goals))
FROM Players
GROUP BY Players.TeamID)
In the TeamAverage column I want to display the average goals' number per team, but the query only gets the average number of goals for the first team.
Who can I iterate through this select
(SELECT ROUND(AVG(Players.Goals)) FROM Players GROUP BY Players.TeamID)
and get each result according to the teams' ID?