Oracle 12c-删除记录

时间:2018-11-19 22:13:56

标签: sql oracle oracle12c sql-delete

我有将近500万条记录,如下所示:

0100000000 112 at this label: 0.00
0100000000 745 at this label: 0.00
0100000000 312 at this label: 0.00

我只需要删除两个记录中具有相同EMPLOYEE_ID LOG_DATE DETAIL_LOG ----------- -------- ---------- 00001 19/11/2018 12:03:37 Database user USER1; Department ID from '12345' to ''; 00001 19/11/2018 12:03:37 Database user USER1; Department ID from '' to '12345'; 00002 19/11/2018 12:02:06 Database user USER1; Department ID from '12345' to ''; 00002 19/11/2018 12:02:07 Database user USER1; Department ID from '' to '12345'; 00003 19/11/2018 07:22:10 Database user USER1; Department ID from '99999' to ''; 00003 19/11/2018 07:22:11 Database user USER1; Department ID from '' to '99999'; 00004 19/11/2018 09:40:11 Database user USER1; Department ID from '99999' to ''; 00004 19/11/2018 09:40:12 Database user USER1; Department ID from '' to '22222'; 的这些记录。因此,除了department id employee_id以外,所有这些都需要删除。我该怎么办?

1 个答案:

答案 0 :(得分:1)

try {
    // do something
} catch (Exception e) {
    try {
        Throwable exc = ExceptionUtils.getRootCause(e);
        if (exc instanceof MyOwnException) {
            // do something
        } 
    }
}

输出:

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;