If you are SQL 2005+, the CTE
approach will work:
WITH salesWithRank AS ( SELECT CompanyID, Year, Sales , RANK() OVER (PARTITION BY CompanyId ORDER BY Sales DESC) AS RowNumber FROM SalesData ) SELECT CompanyID, Year, Sales FROM salesWithRank AS s WHERE RowNumber <= 10
Note that if you have a computer_name in the table, you can use this to order as well.
source share