Pasakiet, kāds, lūdzu - KĀDA VELNA PĒC TAS STRĀDĀ???!!!
SELECT
change_date AS change_date
,date_from AS date_from
,date_to AS date_to
,cgp_id
FROM (select 1 as ctr_id
,to_date('20070101','yyyymmdd') as change_date
,to_date('20071106','yyyymmdd') as date_from
,1 as cgp_id
,null as date_to
from dual
) cgp_new
JOIN (select 1 as id, 'C' as code from dual) cgp
ON (cgp_new.cgp_id = cgp.id)
JOIN (select 1 as ctr_id
,to_date('20070201','yyyymmdd') as change_date
,to_date('20071106','yyyymmdd') as date_from
,2 as cgp_id
,to_date('20071105235959','yyyymmddhh24miss') as date_to
from dual
) cgp_old
ON ( cgp_old.ctr_id = cgp_new.ctr_id
AND cgp_old.date_to = cgp_new.date_from - 1/(24*60*60) -- intervāli nepārklājas
)
JOIN (select 2 as id, 'D' as code from dual) cgp2
ON (cgp2.id = cgp_old.cgp_id)
/