I have this table:
Team | Player
Barca | Messi
Chelsea | Lampard
Barca | Messi
Barca | Messi
Chelsea | Lampard
Chelsea | Drogba
Barca | Iniesta
Barca | Xavi
Barca | Iniesta
Barca | Puyol
So I need a query that retrieves the top two common players in each team. something like this:
Team | Player
Barca | Messi
Barca | Iniesta
Chelsea | Lampard
Chelsea | Drogba
You can use a subquery/CTE to calculate the counts of each player and the ranking, by numbering rows according to descending order of count per team. Then pull out the top two players from each team:
Query (demo):
Results: