create table oldtable
(
fileNo varchar(10),
folder varchar(10),
fileType varchar(10),
col_1 varchar(10),
col_2 varchar(10),
col_3 varchar(10),
col_4 varchar(10)
)
Table created.
INSERT INTO oldtable (fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('ABC' , 'Folder1','Type1' ,'111' ,'111','111' ,null)
1 row(s) inserted.
INSERT INTO oldtable (fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('ABC' , 'Folder1','Type2' ,'111' ,'111','111',null)
1 row(s) inserted.
INSERT INTO oldtable (fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('GHI' , 'Folder3','Type3' ,'333' ,'333','333','333')
1 row(s) inserted.
INSERT INTO oldtable (fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('JKL' , 'Folder4','Type3' ,'444' ,'444','444','444')
1 row(s) inserted.
INSERT INTO oldtable (fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('MNO' , 'Folder5','Type4' ,'555' ,'555' ,'555' ,'555')
1 row(s) inserted.
INSERT INTO oldtable (fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('PQR' , 'Folder6','Type4' ,'666' ,'666' ,'666' ,'666')
1 row(s) inserted.
INSERT INTO oldtable (fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4 ) VALUES ('STU' , 'Folder7','Type5' ,'777' ,'777','777','777')
1 row(s) inserted.
create table newtable
(
fileNo varchar(10),
folder varchar(10),
fileType varchar(10),
col_1 varchar(10),
col_2 varchar(10),
col_3 varchar(10),
col_4 varchar(10),
nid int
)
Table created.
INSERT INTO newtable(fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4, NID ) VALUES ('ABC' , 'Folder1', null ,'111' ,'111','111' ,null, 1)
1 row(s) inserted.
INSERT INTO newtable(fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4, NID ) VALUES ('ABC' , 'Folder1', null,'111' ,'111','111',null, 2)
1 row(s) inserted.
INSERT INTO newtable(fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4, NID ) VALUES ('GHI' , 'Folder3', null ,'333' ,'333','333','333', 3)
1 row(s) inserted.
INSERT INTO newtable(fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4, NID ) VALUES ('JKL' , 'Folder4',null ,'444' ,'444','444','444', 4)
1 row(s) inserted.
INSERT INTO newtable(fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4, NID ) VALUES ('MNO' , 'Folder5',null ,'555' ,'555' ,'555' ,'555', 5)
1 row(s) inserted.
INSERT INTO newtable(fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4, NID ) VALUES ('PQR' , 'Folder6',null ,'666' ,'666' ,'666' ,'666', 6)
1 row(s) inserted.
INSERT INTO newtable(fileNo , folder, fileType, COL_1 ,COL_2 ,COL_3 ,COL_4, NID ) VALUES ('STU' , 'Folder7',null ,'777' ,'777','777','777', 7)
1 row(s) inserted.
update
( with
oldt as
( select fileNo , folder, fileType,
row_number() over (partition by fileNo , folder
order by fileType)
as rn
from oldtable
),
newt as
( select fileNo , folder, fileType, nid,
row_number() over (partition by fileNo , folder
order by fileType)
as rn
from newtable
),
upd as
( select
n.nid,
o.fileType as old_fileType
from newt n
join oldt o
on n.fileNo = o.fileNo
and n.folder = o.folder
and n.rn = o.rn
)
select
up.fileType,
( select upd.old_fileType
from upd
where upd.nid = up.nid
) as old_fileType
from newtable up
) x
set fileType = old_fileType
7 row(s) updated.
select *
from newtable
| FILENO | FOLDER | FILETYPE | COL_1 | COL_2 | COL_3 | COL_4 | NID | ABC | Folder1 | Type1 | 111 | 111 | 111 | - | 1 | ABC | Folder1 | Type2 | 111 | 111 | 111 | - | 2 | GHI | Folder3 | Type3 | 333 | 333 | 333 | 333 | 3 | JKL | Folder4 | Type3 | 444 | 444 | 444 | 444 | 4 | MNO | Folder5 | Type4 | 555 | 555 | 555 | 555 | 5 | PQR | Folder6 | Type4 | 666 | 666 | 666 | 666 | 6 | STU | Folder7 | Type5 | 777 | 777 | 777 | 777 | 7 |
|---|