Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using alias from JOIN statement in FROM

I'm trying to build a PostGIS query that uses an alias of the JOIN statement in the FROM statement, but that is not possible. Is there another way to do it?

SELECT DISTINCT 
ST_Buffer(
    ST_Centroid(geom),
    ST_Area(geom))
FROM building AS b, ST_Transform(sg.geometry, 31467) AS geom 
LEFT JOIN surface_geometry AS sg ON b.id = sg.cityobject_id WHERE ST_Area(geom) < 100
like image 795
megges Avatar asked Jan 25 '12 18:01

megges


2 Answers

You can introduce an extra level of subquery.

So currently you have:

SELECT DISTINCT ST_Buffer( ST_Centroid(geom), ST_Area(geom) )
FROM building AS b,
     ST_Transform(sg.geometry, 31467) AS geom 
     LEFT JOIN surface_geometry AS sg ON b.id = sg.cityobject_id
WHERE ST_Area(geom) < 100

(I changed the indentation to make a bit more sense of it, although I still don't understand it: you don't seem to use the values from b anywhere). You need something like:

SELECT DISTINCT ST_Buffer( ST_Centroid(geom), ST_Area(geom) )
FROM (
    SELECT ST_Transform(sg.geometry, 31467) AS geom 
    FROM building AS b
         LEFT JOIN surface_geometry AS sg ON b.id = sg.cityobject_id
) x
WHERE ST_Area(geom) < 100
like image 76
araqnid Avatar answered Oct 18 '22 23:10

araqnid


Why would you not try:

SELECT DISTINCT 
    ST_Buffer(ST_Centroid(ST_Transform(sg.geometry, 31467),
              ST_Area(ST_Transform(sg.geometry, 31467)))
FROM building b LEFT JOIN surface_geometry sg ON (b.id = sg.cityobject_id)
WHERE ST_Area(ST_Transform(sg.geometry, 31467)) < 100;

It is also possible that this might work:

SELECT DISTINCT 
    ST_Buffer(ST_Centroid(geom), ST_Area(geom))
FROM building b LEFT JOIN surface_geometry sg ON (b.id = sg.cityobject_id) INNER JOIN
     ST_Transform(surface_geometry.geometry, 31467) geom ON (ST_Area(geom) < 100);

You might want to verify the placement of () as at the moment I have no place to test it.

like image 33
Karlson Avatar answered Oct 18 '22 22:10

Karlson