I have several tables containing data on the players and games that they played this season at the bowling center during the league. To do this, use a specific query: sort the upper X values ββthis year for men and women. I have it all, but I still have a problem in a particular case, when some players play in several leagues and have more than one of their average values ββin the upper X.
Obviously, I only want to list the best average for this player, so if player A has the best average with 200 in the ABC League, as well as the second best average with 198 in the DEF League, I want only 200 of those listed.
Here is a simplified version of the query that I would like to change, because now I need to delete duplicates manually or I have to write the sorter in another language, but I would prefer to do it in pure SQL. (I removed the unnecessary information from the request for this example):
SELECT playerId, ROUND(AVG(score),2)Average, season, leagueName, COUNT(score)NumGames FROM Scores WHERE season = '2011-2012' AND score > -1 GROUP BY season, playerID, leagueName ORDER BY Average DESC LIMIT 0,30
Basically, the Scores
table contains each individual game, playerId, the season in which the game was played, and the league name (and other columns that are not required in this example).
WHERE
must make sure that the game was played this season and that the rating is positive (-1 for those when people are absent). I group everything by season, playerID and leagueName, so I get the average PER LEAGUE for each player, and not the average of all games played in different leagues.
I tried to use the DISTINCT
keyword, but this does not work because I cannot use DISTINCT
for only one column. I also tried other things, but none of them came close to work, so I wonder if it is possible to do this or will I have to resort to using another language to sort this result set and remove duplicates?