declare @T table
(
IdentityId int,
ProductId int,
TypeId int,
Rating int
)
insert into @T values
(3, 1, 1, 9),
(7, 1, 2, 3),
(9, 500, 1, 7),
(2, 500, 2, 5),
(2, 777, 4, 5),
(12, 777, 3, 8),
(2, 999, 4, 1)
;with C as
(
select ProductId,
TypeId,
Rating,
row_number() over(partition by ProductID
order by Rating desc) as rn
from @T
)
select ProductId,
TypeId,
Rating
from C
where rn = 1
order by ProductId