ここでのキーワードはFIRST 。分析関数FIRST_VALUE
を使用できます または集計コンストラクトFIRST
。FIRST
の場合 またはLAST
パフォーマンスは、同等のFIRST_VALUE
よりも悪くなることはなく、多くの場合、優れています。 またはLAST_VALUE
余分なウィンドウソートがなく、その結果、実行コストが低くなるため、構築します。
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 */
;
12c以降、演算子LATERAL
が導入されました 、およびCROSS/OUTER APPLY
結合、JOIN
の右側で相関サブクエリを使用できるようにします 条項:
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 */
;