declare @t table
(
tid int,
tv char(1)
)
insert into @t
select 1,'a' union all
select 1,'a' union all
select 2,'b' union all
select 3,'m' union all
select 4,'b' union all
select 5,'c' union all
select 6,'m' union all
select 7,'v' union all
select 8,'m' union all
select 9,'n'
select distinct *,(select count(1) from @t where tid<=t.tid and tv=t.tv) c
from @t t