Требуется курсор, особенно если идентификатор не гарантирует, что он будет последовательным и без пробелов.
Это будет работать в MS- SQL:
-- stage sample data
drop table if exists oldTable
create table oldTable (id int, old_type nvarchar(1))
go
insert into oldTable values (1, null), (2, 'A'), (3, null), (4, null), (5, 'A'), (6, null), (7, 'B'), (8, 'A'), (9, 'B'), (10, null)
go
-- get new table ready
drop table if exists newTable
create table newTable (
id int,
old_type nvarchar(1),
new_type nvarchar(1)
)
GO
-- prepare for lots of cursing
declare @the_id int
declare @the_type nvarchar(1)
declare @running_type nvarchar(1)
declare mycursor cursor for
select
id, old_type
from
oldTable
-- do a barrel roll
open mycursor
fetch mycursor into @the_id, @the_type
while @@ERROR = 0 and @@FETCH_STATUS = 0
begin
set @running_type = COALESCE(@the_type, @running_type)
insert into newTable(id, old_type, new_type)
values (@the_id, @the_type, @running_type)
fetch mycursor into @the_id, @the_type
end
close mycursor
deallocate mycursor
go
-- verify results
select * from newTable
go