select dr3_source_id, dr2_source_id,
angular_distance, magnitude_difference, pm
from edr3.gaia_source as gaia3
join edr3.dr2_neighbourhood as xm
on gaia3.source_id = xm.dr3_source_id
where pm > 100
select xm2.dr2_source_id, xm2.dr3_source_id,
xm2.angular_distance, xm2.magnitude_difference, pm from (
select dr3_source_id, min(angular_distance) as min_angular_distance
from edr3.gaia_source as gaia3_1
join edr3.dr2_neighbourhood as xm1
on gaia3_1.source_id = xm1.dr3_source_id
where pm > 100
group by dr3_source_id
) as subquery
join edr3.dr2_neighbourhood as xm2
on xm2.dr3_source_id = subquery.dr3_source_id
and xm2.angular_distance = subquery.min_angular_distance
join edr3.gaia_source as gaia3_2
on gaia3_2.source_id = subquery.dr3_source_id