create table tb1 
(
wn varchar(30),
no int,
qty int
)insert into tb1
select 'w01',1,5 union all
select 'w01',1,3 union all
select 'w01',1,4 union all
select 'w02',1,5 union all
select 'w02',1,1 union all
select 'w03',1,5 union all
select 'w03',1,6
goselect identity(int,1,1) as id ,* into #temp from tb1 order by wnselect wn,(select count(*) from #temp where wn=t1.wn and id <=t1.id ) as no ,qty from #temp t1drop table #temp,tb1