create table table1( a int,    b int)
insert table1 select 11,    1     
union all select 11,    1
union all select 11,    1
union all select 22,    1
union all select 22,    1
union all select 22,    1
union all select 22,    1
union all select 33,    1
union all select 33,    1
select id=identity(int,1,1), a,b into # from table1 
select a,(select count(*) from # b where b.id<=a.id and b.a=a.a) from # a
drop table #