2016-08-08 16 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')); 

を削除するレコードの量もTICKET_IDあたり2よりも大きくてもよいです。

どうすればいいですか?

+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 
); 
関連する問題