create table table1
(group_name varchar(10),
seq_id int)
insert table1 select 'aaa',NULL
union all select 'aaa',NULL
union all select 'aaa',NULL
union all select 'bbb',NULL
union all select 'bbb',NULL
union all select 'ccc',NULL
select id=identity(int),* into #t from table1
update #t set seq_id=(select count(*) from #t where group_name=t.group_name and id<=t.id) from #t t
select group_name,seq_id from #t
create table tb3
(id int identity(1,1),
group_name varchar(100),
seq_id int
)
insert into tb3
select 'aaa',null union all
select 'aaa',null union all
select 'bbb',null union all
select 'bbb',null union all
select 'aaa',null union all
select 'ccc',null
update tb3
set seq_id =(select count(*) from tb3 a where tb3.id >=a.id and tb3.group_name=a.group_name)
select group_name,seq_id from tb3
order by group_name,seq_id