Search code examples
sqlpostgresqlpostgis

SQL : combine 2 statements with 2 SELECT


How can I combine these 2 SQL statement into a one?

I would like to replace the value shape here:

SELECT path[1] AS number, st_x(geom), st_y(geom) 
FROM st_dumppoints(shape)

by this :

SELECT shape 
FROM tableA 
WHERE id = 'test'

Something like this:

SELECT path[1] AS number, st_x(geom), st_y(geom) 
FROM st_dumppoints(SELECT shape FROM tableA WHERE id = 'test')

Solution

  • Just add an extra pair of parentheses around the subselect. Syntactically, a subquery needs parentheses, and the function invocation too, so you end up with

    FROM st_dumppoints((SELECT shape FROM tableA WHERE id = 'test'))
    

    Note that the statement will cause an error if the subquery returns more than one row.