更新列问题
小弟不才 有以下两列:
lie1 lie2
A 0
A 0
A 0
B 0
B 0
B 0
B 0
C 0
C 0
D 0
D 0
D 0
E 0
如何更新成:
lie1 lie2
A 0
A 1
A 2
B 0
B 1
B 2
B 3
C 0
C 1
D 0
D 1
D 2
E 0
谢谢!各位!
------解决方案--------------------declare @table table (ie1 char(1),lie2 int)
insert into @table
select 'A',0 union all
select 'A',0 union all
select 'A',0 union all
select 'B',0 union all
select 'B',0 union all
select 'B',0 union all
select 'B',0 union all
select 'C',0 union all
select 'C',0 union all
select 'D',0 union all
select 'D',0 union all
select 'D',0 union all
select 'E',0
;
with a as
(
select *,ROW_NUMBER() OVER(PARTITION BY ie1 order by lie2) id from @table
)
update a set lie2=id-1
select * from @table
------解决方案--------------------尝试了一下用一条语句解决此问题,但没能成功。
------解决方案--------------------
if object_id('Tempdb..#t') is not null drop table #t