2016-08-08 134 views
0

由於在填充數據表時出現錯誤,現在表中存在錯誤的記錄。特別是有重疊的時間戳。現在我想刪除的記錄,這在下面的例子中所示的特性:Oracle - 刪除重疊記錄

enter image description here

Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('1','3',to_timestamp('27.05.2016 17:27:08','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('31.05.2016 18:34:16','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('2','3',to_timestamp('31.05.2016 18:34:16','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('31.05.2016 18:34:26','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('3','3',to_timestamp('31.05.2016 18:34:26','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('01.06.2016 08:56:51','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('4','3',to_timestamp('01.06.2016 08:56:51','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('01.06.2016 18:38:30','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('5','3',to_timestamp('01.06.2016 18:38:30','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('08.06.2016 15:23:22','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('6','3',to_timestamp('08.06.2016 15:23:22','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('08.06.2016 21:02:21','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('7','3',to_timestamp('08.06.2016 17:09:02','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('08.06.2016 19:04:05','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('8','3',to_timestamp('08.06.2016 19:04:05','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('08.06.2016 21:02:21','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('9','3',to_timestamp('08.06.2016 21:02:21','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('09.09.9999 00:00:00','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('10','6',to_timestamp('20.06.2016 21:23:48','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('20.06.2016 21:23:56','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('11','6',to_timestamp('20.06.2016 21:23:56','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('21.06.2016 08:59:16','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('12','6',to_timestamp('21.06.2016 08:22:35','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('21.06.2016 08:23:02','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('13','6',to_timestamp('21.06.2016 08:23:02','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('21.06.2016 08:59:16','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('14','6',to_timestamp('21.06.2016 08:59:16','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('21.06.2016 12:06:49','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('15','6',to_timestamp('21.06.2016 12:06:49','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('22.06.2016 06:29:51','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('16','6',to_timestamp('22.06.2016 06:29:51','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('22.06.2016 08:18:18','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('17','6',to_timestamp('22.06.2016 08:18:18','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('22.06.2016 08:24:58','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('18','6',to_timestamp('22.06.2016 08:24:58','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('22.06.2016 08:38:18','DD.MM.RR HH24:MI:SSXFF')); 
Insert into TEST_OVERLAP (TICKET_SK,TICKET_ID,VALID_FROM,VALID_TO) values ('19','6',to_timestamp('22.06.2016 08:38:18','DD.MM.RR HH24:MI:SSXFF'),to_timestamp('09.09.9999 00:00:00','DD.MM.RR HH24:MI:SSXFF')); 

的記錄刪除量也可能比2%TICKET_ID大。

這怎麼辦?

+2

告訴我們您已經嘗試 – davegreen100

+0

請郵寄樣本數據格式的文本,而不是圖像什麼 – Aleksej

回答

2

如果我正確理解您要刪除記錄的每個記錄具有相同的ticket_id以及完全包含該記錄的時間段。

下面是做這項工作的查詢:

DELETE tickets t 
WHERE EXISTS (
    SELECT * 
    FROM tickets t2 
    WHERE t2.ticket_id = t.ticket_id 
     AND t2.valid_from < t.valid_from 
     AND t2.valid_to >= t.valid_to 
);