I'm trying to run this query but I get "ORA-00904: "Z1"."LONGITUDE": invalid identifier"
Is there a way to rewrite this so I have access to that column in the exists subquery? Or is there generally a better way to achieve what I'm trying to do?
Thanks
select zip, count(UNIQUE address_id) LOCATIONS
from records
inner join addresses a using(address_id)
inner join zip_coords z1 using(zip)
where exists
(
select 1 from (
select distance(z1.latitude, z1.longitude, z2.latitude, z2.longitude) d
from zip_coords z2
where z2.zip in (
select zip from available_zips
)
) where d <= 50
)
GROUP BY ZIP