Kluczowym słowem jest tutaj PIERWSZY . Możesz użyć funkcji analitycznej FIRST_VALUE lub zagregować konstrukcję FIRST .
Dla FIRST lub LAST wydajność nigdy nie jest gorsza i często lepsza niż odpowiednik FIRST_VALUE lub LAST_VALUE skonstruować, ponieważ nie mamy zbędnego sortowania okien, a w konsekwencji niższy koszt wykonania:
select table_A.id, table_A.name, firstFromB.city
from table_A
join (
select table_B.id2, max(table_B.city) keep (dense_rank first order by table_B.city) city
from table_b
group by table_B.id2
) firstFromB on firstFromB.id2 = table_A.id
where 1=1 /* some conditions here */
;
Od 12c wprowadzono operator LATERAL , a także CROSS/OUTER APPLY złączenia, umożliwiają użycie skorelowanego podzapytania po prawej stronie JOIN klauzula:
select table_A.id, table_A.name, firstFromB.city
from table_A
cross apply (
select max(table_B.city) keep (dense_rank first order by table_B.city) city
from table_b
where table_B.id2 = table_A.id
) firstFromB
where 1=1 /* some conditions here */
;