lastleaf 回复 wangjiaojiao(提问者)CREATE TABLE [dbo].[tab](
code int NULL,
tag varchar(10) NULL
)
insert into [dbo].[tab] values(1,\'A\')
insert into [dbo].[tab] values(1,\'B\')
insert into [dbo].[tab] values(1,\'C\')
insert into [dbo].[tab] values(2,\'r\')
insert into [dbo].[tab] values(2,\'f\')
insert into [dbo].[tab] values(2,\'v\')
insert into [dbo].[tab] values(2,\'d\')
ALTER TABLE tab ADD id int --插入字段
update tab set id
= (select id from (select ROW_NUMBER() OVER(PARTITION BY code ORDER BY tag DESC) as id,tag from tab) a
where a.tag = tab.tag
) --更新字段
select * from tab
这样就是你要的效果