Gunakan agregasi:
select foreignKey, min(primaryKey)
from t
group by foreignKey;
Jika Anda menginginkan nilai acak, Anda dapat menggunakan keep
sintaks:
select foreignKey,
min(primaryKey) keep (dense_rank first order by dbms_random.random)
from t
group by foreignKey;
Dan jika Anda memiliki banyak kolom, Anda dapat menggunakan row_number()
atau subkueri terkait:
select t.*
from t
where t.primarykey = (select min(t2.primarykey) from t t2 where t2.foreignkey = t.foreignkey);