DECLARE @temp TABLE(
id INT,
[name] VARCHAR(50),
class VARCHAR(50)
)
INSERT INTO @temp
SELECT 1,'a','A'
UNION ALL SELECT 2,'b','C'
UNION ALL SELECT 3,'c','B'
UNION ALL SELECT 4,'d','C'
UNION ALL SELECT 5,'e','B'
UNION ALL SELECT 6,'f','A'
SELECT * FROM @temp AS _temp WHERE [name] IN
(
SELECT TOP 1 [name] FROM @temp WHERE _temp.class=class ORDER BY id DESC
)
ORDER BY class