sql - Oracle - Deleting overlapping records -


due mistake while populating table data there erroneous records in table now. in particular, there overlapping timestamps. want delete records, have characteristics shown in following example:

enter image description here

insert 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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')); 

the amount of records delete larger 2 per ticket_id.

how done?

if correctly understood want delete every record have record same ticket_id , timeframe contains record.

here's query job:

delete tickets t exists (     select *     tickets t2     t2.ticket_id  =  t.ticket_id       , t2.valid_from <  t.valid_from       , t2.valid_to   >= t.valid_to ); 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -