SELECT DISTINCT serverId FROM MatchesList WHERE Id in (SELECT TOP 100 PERCENT matchId FROM MatchesPlayers WHERE name='Player0') ORDER BY COUNT(*)OVER(PARTITION BY serverId) DESC, serverId 

Such a request. There is a MatchesPlayers table where I want to select all matchId entries. Then I want to select all serverId from MatchesList so that I have a list of unique serverId sorted by their number. All is good, but the error

 ORDER BY items must appear in the select list if SELECT DISTINCT is specified. 

    1 answer 1

    Everything is a bit simpler, window functions and distinct to nothing, the usual group by does the same thing:

     SELECT serverId FROM MatchesList WHERE Id in (SELECT TOP 100 PERCENT matchId FROM MatchesPlayers WHERE name='Player0') GROUP BY serverId ORDER BY COUNT(*) DESC, serverId