create table test (CPBH char(7),LSH int default 0)
insert test(CPBH) select '6666122'
insert test(CPBH) select '6666122'
insert test(CPBH) select '6666122'
insert test(CPBH) select '6666133'
insert test(CPBH) select '6666133'
insert test(CPBH) select '5454465'
insert test(CPBH) select '5451165'
insert test(CPBH) select '4541165'
insert test(CPBH) select '4541165'
insert test(CPBH) select '5555511'
--select * from test
alter table test
add id int identity(1,1)
go
update test set
LSH=
(
select count(*)+1 from test where cpbh=a.cpbh and id<a.id ----内层表为引用表,因为肯定有一条记录不符合id<a.id, 所以0,1,2
) from test a ------外层表
alter table test
drop column id
go
select * from test
drop table test