what do you mean it doesn't work?? of course it does. do you have any idea how this works?you shouldn't just copy paste it. understand it first.Declare @myTable table (category varchar(100), subcategory varchar(100), rank int )Insert Into @myTable Select 'A', 'a', 2 Union All Select 'A', 'b', 52 Union All Select 'A', 'c', 14 Union All Select 'A', 'd', 46Union All Select 'A', 'e', 37 Union All Select 'A', 'f', 95 Union All Select 'A', 'g', 73 Union All Select 'A', 'h', 67 Union All Select 'A', 'i', 80 Union All Select 'A', 'j', 03 Union All Select 'B', 'a', 18 Union All Select 'B', 'b', 44 Union All Select 'B', 'c', 52 Union All Select 'B', 'd', 60 Union All Select 'B', 'e', 28 Union All Select 'B', 'f', 06 Union All Select 'C', 'g', 70 Union All Select 'C', 'h', 90 Union All Select 'C', 'i', 89 Union All Select 'C', 'j', 31declare @n intSet @n = 2Select category, subcategory, rankFrom @myTable as t1Where (Select count(distinct rank) From @myTable Where category=t1.category and rank <= t1.rank)<=@nOrder By category, Rank Desc
Go with the flow & have fun! Else fight the flow