Try:
declare @Product as table (id int, name nvarchar(20)) declare @Score as table (id int, ProductID int, ScoreValue decimal(23, 5)) insert into @Product values (1, 'a'), (2, 'b'), (3, 'c') insert into @Score values (1, 1, 25), (2, 1, 30), (3, 2, 40), (4, 2, 45), (5, 3, 3) select distinct top 2 name, ProductID, AVG(ScoreValue) over (partition by name) from @Product a inner join @Score b on a.id=b.ProductID order by 3 desc
Change the table name and number of rows.
source share