I have a situation similar to the following
declare @TestTable as table(ID int identity(1,1), Column1 varchar(2), Column2 int null)insert into @TestTable(Column1, Column2)values('1',null),('1',null),('1',111),('1',null),('2',222),('2',null),('2',null),('3',null),('3',null),('3',null)select *from @TestTable
Output
ID Column1 Column2----------- ------- -----------1 1 NULL2 1 NULL3 1 1114 1 NULL5 2 2226 2 NULL7 2 NULL8 3 NULL9 3 NULL10 3 NULL
I want to update the Column2 with a sequential number for each value of Column1.If there is already an existing number in Column2 for a value of Column1, I want to take the next available number.
Expected Output
ID Column1 Column2----------- ------- -----------1 1 1122 1 1133 1 1114 1 1145 2 2226 2 2237 2 2248 3 19 3 210 3 3
I was hoping I could use something like this
UPDATE @TestTableSET Column2 = (SELECT COALESCE(MAX(Column2),0) + 1 FROM @TestTable WHERE Column1 = '???')
But I don't know how to point to Column1 of the same table.Any help would be much appreciated!