add batch remove batch split batch comment selection show hidden batches hide batch highlight batch
db<>fiddle
donate feedback about
By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
create table table_ent (cod_prod, date_ent) as
select 362, to_date('14/09/2015', 'DD/MM/YYYY') from dual
union all select 362, to_date('15/09/2015', 'DD/MM/YYYY') from dual
union all select 362, to_date('16/09/2015', 'DD/MM/YYYY') from dual
union all select 362, to_date('05/10/2016', 'DD/MM/YYYY') from dual
union all select 362, to_date('06/10/2016', 'DD/MM/YYYY') from dual
union all select 362, to_date('07/10/2016', 'DD/MM/YYYY') from dual
union all select 362, to_date('08/10/2016', 'DD/MM/YYYY') from dual
union all select 362, to_date('23/10/2016', 'DD/MM/YYYY') from dual
union all select 362, to_date('24/10/2016', 'DD/MM/YYYY') from dual
union all select 362, to_date('25/10/2016', 'DD/MM/YYYY') from dual
union all select 362, to_date('30/10/2019', 'DD/MM/YYYY') from dual;
11 rows affected
create table table_out (cod_prod, date_out) as
select 362, to_date('01/10/2016', 'DD/MM/YYYY') from dual
union all select 362, to_date('07/10/2016', 'DD/MM/YYYY') from dual
union all select 362, to_date('29/10/2016', 'DD/MM/YYYY') from dual;
3 rows affected
select o.cod_prod, o.date_out, e.cod_prod, max(e.date_ent) as date_ent
from table_out o
join table_ent e on e.cod_prod = o.cod_prod and e.date_ent < o.date_out
group by o.cod_prod, o.date_out, e.cod_prod
order by o.cod_prod, o.date_out;
COD_PROD DATE_OUT COD_PROD DATE_ENT
362 01-OCT-16 362 16-SEP-15
362 07-OCT-16 362 06-OCT-16
362 29-OCT-16 362 25-OCT-16