由于在使用数据填充表时出错,现在表中存在错误记录。特别是,存在重叠的时间戳。现在我想删除记录,这些记录具有以下示例中显示的特征:
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个。
怎么可以这样做?
答案 0 :(得分: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
);