sql如何在指定的一列字段中添加自增数字
如图1,对应WG55的A1.2在wg56一列中添加001~006在对应A1.3的wg56中增加001~008。求sql语句...
如图1,对应WG55的A1.2在wg56一列中添加001~006在对应A1.3的wg56中增加001~008。求sql语句
展开
1个回答
展开全部
--测试环境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
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询