--测试环境MSSQL2008
--建表
create table T
(
WG55 Varchar(10),
WG56 Varchar(10),
WG57 Varchar(10)
)
--插入数据
insert into T values('A.12','','001')
insert into T values('A.12','','002')
insert into T values('A.12','','003')
insert into T values('A.11','001','004')
insert into T values('A.11','002','005')
insert into T values('A.11','','006')
insert into T values('A.11','003','007')
insert into T values('A.13','','008')
insert into T values('A.13','','009')
--查询
select WG55,Case when WG56='' then
right('000'+CAST( ROW_NUMBER() over(partition by WG55 order by WG56 desc,WG57) as varchar(10)),3)
else WG56 end As WG56,WG57 from T
--更新
Update T Set WG56=M.WG56
from (select WG55,Case when WG56='' then
right('000'+CAST( ROW_NUMBER() over(partition by WG55 order by WG56 desc,WG57) as varchar(10)),3)
else WG56 end As WG56,WG57 from T ) M
where T.WG57=M.WG57
![](https://video.ask-data.xyz/img.php?b=https://iknow-pic.cdn.bcebos.com/37d3d539b6003af3bf903bfe302ac65c1038b602?x-bce-process=image%2Fresize%2Cm_lfit%2Cw_600%2Ch_800%2Climit_1%2Fquality%2Cq_85%2Fformat%2Cf_auto)