Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres SELECT with multiple select as

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!

like image 791
Matt Avatar asked Dec 04 '25 14:12

Matt


1 Answers

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

like image 115
bma Avatar answered Dec 06 '25 07:12

bma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!