create table mytab(eid varchar2(5), logdate date, log varchar2(200));
insert into mytab values('00001',to_date('19/11/2018 12:03:37','dd/mm/yyyy hh24:mi:ss'),'Database user USER1; Department ID from ''12345'' to '''';');
insert into mytab values('00001',to_date('19/11/2018 12:03:37','dd/mm/yyyy hh24:mi:ss'),'Database user USER1; Department ID from '''' to ''12345'';');
insert into mytab values('00002',to_date('19/11/2018 12:02:06','dd/mm/yyyy hh24:mi:ss'),'Database user USER1; Department ID from ''12345'' to '''';');
insert into mytab values('00002',to_date('19/11/2018 12:02:07','dd/mm/yyyy hh24:mi:ss'),'Database user USER1; Department ID from '''' to ''12345'';');
insert into mytab values('00003',to_date('19/11/2018 07:22:10','dd/mm/yyyy hh24:mi:ss'),'Database user USER1; Department ID from ''99999'' to '''';');
insert into mytab values('00003',to_date('19/11/2018 07:22:11','dd/mm/yyyy hh24:mi:ss'),'Database user USER1; Department ID from '''' to ''99999'';');
insert into mytab values('00004',to_date('19/11/2018 09:40:11','dd/mm/yyyy hh24:mi:ss'),'Database user USER1; Department ID from ''99999'' to '''';');
insert into mytab values('00004',to_date('19/11/2018 09:40:12','dd/mm/yyyy hh24:mi:ss'),'Database user USER1; Department ID from '''' to ''22222'';');
commit;
select * from mytab;
Выход:
EID LOGDATE LOG
00001 19-NOV-18 Database user USER1; Department ID from '12345' to '';
00001 19-NOV-18 Database user USER1; Department ID from '' to '12345';
00002 19-NOV-18 Database user USER1; Department ID from '12345' to '';
00002 19-NOV-18 Database user USER1; Department ID from '' to '12345';
00003 19-NOV-18 Database user USER1; Department ID from '99999' to '';
00003 19-NOV-18 Database user USER1; Department ID from '' to '99999';
00004 19-NOV-18 Database user USER1; Department ID from '99999' to '';
00004 19-NOV-18 Database user USER1; Department ID from '' to '22222';
Решение:
delete from mytab
where eid in (
select eid
from (
select x.eid, max(x.from_dept) max_from_dept, max(x.to_dept) max_to_dept
from (
select eid,
rtrim(ltrim(regexp_substr(log,'(from)[^(to)]+(to)'),'from '),' to') from_dept,
rtrim(ltrim(regexp_substr(log,'(to)[^(;)]+(;)'),'to '),';') to_dept
from mytab) x
group by x.eid) y
where y.max_from_dept = y.max_to_dept);
commit;
select * from mytab;
Выход:
EID LOGDATE LOG
00004 19-NOV-18 Database user USER1; Department ID from '99999' to '';
00004 19-NOV-18 Database user USER1; Department ID from '' to '22222';