I just wonder why people come up with this kind of restrictions?declare @t table (pname varchar(100),Value int,orderseq int)insert into @t values('product1' , 25 , 0)insert into @t values('product1' , 50 , 0)insert into @t values('product1' , 75 ,0)insert into @t values('product1' , 100 ,0)insert into @t values('product4' , 50 , 0)insert into @t values('product4' , 100 , 0)insert into @t values('product4' , 80 , 0)insert into @t values('product7' , 25 , 0)insert into @t values('product8' , 26 ,0) select * from @tdeclare @value varchar(100),@orderseq varchar(100),@pname varchar(100)set @orderseq='1'declare @cnt int=(select COUNT(*)from @t);with cteas( select top (@cnt) * from @t order by pname,Value desc)update cteset orderseq=@orderseq ,@orderseq=case when @pname=pname then @orderseq+1 else 1 end,@pname=pname select * from @t where orderseq=2PBUH