2

I have a table called "gebaeude" with all types of buildings in the city. In the following I want to use a Buffer around the central train station that has the id="2" and a radius of 1000m. Then I want to select the building types "kinergarten" and "school" that are within this radius by intersecting them with the buffer. What I did looks like this:

WITH center AS (
     SELECT ST_Buffer(geom, 1000) FROM gebaeude 
     WHERE id='2')
SELECT gebaeude.name AS "Name",
        ST_Distance(center.geom, gebaeude) AS Distanz
FROM gebaeude, center
WHERE (gebaeude='school' OR gebaeude.type='kindergarten')
AND ST_Intersects(gebaeude.geom,center.geom);

I get the following error:

ERROR: column center.geom does not exist LINE 5: ST_Distance(center.geom, gebaeude) AS Distanz ^

What am I doing wrong?

2
  • 3
    Do not do that. This is one of the most infamous misconceptions in spatial computation. You got an answer using ST_DWithin on one of your older posts, and that is the optimal solution!
    – geozelot
    Commented Nov 21, 2023 at 20:16
  • This isn't really a GIS error or PostGIS issue so much as a PostgreSQL/database one, so you probably want to research in Database Administrators as well. The moral of the story here is to always alias function/expression-based columns (so you don't have to memorize how the unnamed field is mapped according to rules in the DBMS).
    – Vince
    Commented Nov 21, 2023 at 21:55

1 Answer 1

3

If you don't specify column names in the CTE, they will have the same names as in the inner select statement, so here the column would be named st_buffer instead of geom.

You can set the name at the CTE level

WITH center(geom) AS (
     SELECT ST_Buffer(geom, 1000) ...
)

or within the inner select:

WITH center AS (
     SELECT ST_Buffer(geom, 1000) as geom ...
)

But really, don't use a buffer here, it is not the way to go with your issue. Use ST_DWithin instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.