I have a problem with a Postgres query and can't find a solution in stackexchange or any SQL Documentation.
I have two tables in PostGIS, one for streets and one for house-numbers. I now want to find the closest given house number to a certain street (for an address search).
In a sub-query I calculated the distance, selected the street name and the number as well as the coordinates. In the where clause I tried to filter the data for the minimal distance. I addressed the subquery with its alias, but get the error that the "relation is not defined", although I just defined it. Any simple solution for that problem?
SELECT * FROM
(SELECT ST_Distance(ha.geom, sn.geom) AS cp
, sn.nam AS name
, ha.hnr AS nr
, ST_AsText(ST_Transform(ha.geom,4326)) AS pt
FROM hausnummern_be AS ha, strassen_be AS sn
WHERE ha.hnr = '1' AND sn.nam LIKE 'Samariter%'
ORDER BY cp ASC) AS xy
WHERE cp = (SELECT min(cp) FROM xy);