3

I'm trying to use the output from two SELECT statements in a PostGIS function.

What is the correct syntax for doing this? I'm getting a syntax error at or near the second SELECT statement.

SELECT ST_Split(tracks, roads)
FROM
(
    SELECT * FROM (SELECT ST_Buffer(road_geom,50) FROM table1 WHERE a = '' AND b = '') as roads,
    SELECT * FROM (SELECT the_geom FROM table2 WHERE c = '' AND d = '') as tracks

)

Error output:

 ERROR:  syntax error at or near "SELECT"
 LINE 5:  SELECT * FROM (SELECT the_geom FROM table2...
     ^
********** Error **********
ERROR: syntax error at or near "SELECT"
SQL state: 42601
Character: 178

Thanks!

1 Answer 1

6

Demonstrating one method of using a CTE:

WITH    roads as (SELECT ST_Buffer(road_geom,50) as road FROM table1 WHERE a = '' AND b = ''),
        tracks as (SELECT the_geom as track FROM table2 WHERE c = '' AND d = '')
SELECT ST_Split( (select track from tracks), (select road from roads) );

Docs at http://www.postgresql.org/docs/current/static/queries-with.html

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your quick and accurate reply.
One thing to note is that the SELECTs from the "roads" and "tracks" virtual tables (the clauses in the WITH block) -- if they could ever return more than one row the query will fail, so you should then add LIMIT 1 to both of them (in the WITH clause I mean).
Yeah - I ended up wrapping the split in a loop. Thanks again for your help!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.