Один вариант будет использовать max(a.fill_id) over (partition by a.copy_id order by a.fill_id desc)
(, где a
- псевдоним для TableA
):
select copy_id, fill_id, Shipdate
from
(
with TableA( fill_id, copy_id) as
(
select 1,1 from dual union all
select 2,1 from dual union all
select 3,1 from dual union all
select 4,2 from dual union all
select 5,2 from dual
),
TableB( ship_id, fill_id, Shipdate) as
(
select 1,1,date'2018-01-01' from dual union all
select 2,2,date'2018-01-02' from dual union all
select 3,4,date'2018-01-05' from dual union all
select 4,5,date'2018-01-06' from dual
)
select a.copy_id, a.fill_id, b.Shipdate,
max(a.fill_id) over (partition by a.copy_id order by a.fill_id desc)
as max_ship
from TableA a
full outer join TableB b on a.fill_id = b.fill_id
)
where fill_id = max_ship;
COPY_ID FILL_ID SHIPDATE
------- ------- --------------------
1 3 NULL
2 5 06.01.2018 00:00:00
Вышеприведенный запрос упрощен для уже существующегоТаблицы как:
select copy_id, fill_id, Shipdate
from
(
select a.copy_id, a.fill_id, b.Shipdate,
max(a.fill_id) over (partition by a.copy_id order by a.fill_id desc)
as max_ship
from TableA a
full outer join TableB b on a.fill_id = b.fill_id
)
where fill_id = max_ship;
Rextester Demo