我有一个表格MAIN
,在ID
字段上有重复项,如下所示:
ID SYSTEM FLAG FIRST_NAME LAST_NAME TERMDATE
A021 Alpha Y JOHN DOE null
A021 Beta N JOHN DOE 05-Jun-17
C045 Beta Y PETER PARKER null
C045 Omega N PETER PARKER 05-Jan-17
D078 Alpha N TONY STARK 07-Dec-17
D078 Gamma Y TONY STARK null
X039 Gamma Y STEVE ROGERS null
X039 Gamma Y STEVE ROGERS null
如您所见,我在ID字段中有重复项。我想在MAIN表中保留带有空数据的记录,并将其他记录移到重复的表中。所以我希望我的MAIN表的输出看起来像:
ID SYSTEM FLAG FIRST_NAME LAST_NAME TERMDATE
A021 Alpha Y JOHN DOE null
C045 Beta Y PETER PARKER null
D078 Gamma Y TONY STARK null
X039 Gamma Y STEVE ROGERS null
并且应该将重复项移动到DUPLICATE TABLE,该表应如下所示:
ID SYSTEM FLAG FIRST_NAME LAST_NAME TERMDATE
A021 Beta N JOHN DOE 05-Jun-17
C045 Omega N PETER PARKER 05-Jan-17
D078 Alpha N TONY STARK 07-Dec-17
需要注意的是,完全重复的记录未移至重复的表e.x. X039将被完全删除,不会移动到重复的表格。
我无法找到能够达到此结果的脚本。
答案 0 :(得分:1)
您的基本问题由以下人员处理:
insert into duplicates (ID, SYSTEM, FLAG, FIRST_NAME, LAST_NAME, TERMDATE)
select ID, SYSTEM, FLAG, FIRST_NAME, LAST_NAME, TERMDATE
from main
where termdate is not null;
delete from duplicates
where termdate is not null;
delete from duplicates
where rowid not in (select min(m2.rowid)
from main m2
group by ID, SYSTEM, FLAG, FIRST_NAME, LAST_NAME, TERMDATE
);
如果数据库上可能发生任何事情,您可能希望将所有这些包装在事务中。
答案 1 :(得分:1)
又一个选择:
表格内容:
SQL> select * From main order by id;
ID SYSTEM F FIRST_NAME TERMDATE
----- ---------- - ---------- ----------
a021 alpha y john
a021 beta n john 05.06.2017
c045 beta y peter
c045 omega n peter 05.01.2017
d078 alpha n tony 07.12.2017
d078 gamma y tony
x039 gamma y steve
x039 gamma y steve
8 rows selected.
重复:在同一ID内按TERMDATE排名。将它们插入DUPLICATES&从MAIN中删除它们。你不能只是“移动”它们(从这里取出并将放在那里;你必须分两步完成):
SQL> insert into duplicate
2 select id, system, flag, first_name, termdate
3 from (select id, system, flag, first_name, termdate,
4 rank() over (partition by id order by termdate nulls first) rn
5 from main
6 )
7 where rn > 1;
3 rows created.
SQL> delete from main
2 where ( id, system, flag, first_name, termdate) in
3 (select id, system, flag, first_name, termdate
4 from duplicate
5 );
3 rows deleted.
最后,从MAIN中删除剩余的重复项:
SQL> delete from main m
2 where exists (select null from main m1
3 where m1.id = m.id
4 and m1.rowid > m.rowid
5 );
1 row deleted.
结果:
SQL> select * From main order by id;
ID SYSTEM F FIRST_NAME TERMDATE
----- ---------- - ---------- ----------
a021 alpha y john
c045 beta y peter
d078 gamma y tony
x039 gamma y steve
SQL>
SQL> select * from duplicate order by id;
ID SYSTEM F FIRST_NAME TERMDATE
----- ---------- - ---------- ----------
a021 beta n john 05.06.2017
c045 omega n peter 05.01.2017
d078 alpha n tony 07.12.2017
SQL>