Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling a function on every row returned by a subquery

I need to run the following query to extract the values of my raster records in a specific point.

select st_value((select rast from mytable),
    (select st_GeomFromText('POINT(30.424 -1.978)', 4326)))

But I encounter with the following error:

ERROR: more than one row returned by a subquery used as an expression SQL state: 21000

It needs just one record for this function but I need to extract values of all of records.

like image 564
f.ashouri Avatar asked Mar 18 '26 13:03

f.ashouri


1 Answers

If a subquery returns multiple rows, you must either use it in a common table expression (CTE / WITH query) and FROM alias, or use FROM (SELECT ...) alias. In this case, though, it looks like it's simpler than that:

select st_value(rast, st_GeomFromText('POINT(30.424 -1.978)', 4326))
FROM mytable;

Both subqueries appear to be unnecessary.

If you truly needed the subquery you'd write something syntactically like:

WITH sq(rast) AS ( SELECT rast FROM mytable )
SELECT st_value(rast, st_GeomFromText('POINT(30.424 -1.978)', 4326))
FROM sq;

or

SELECT st_value(rast, st_GeomFromText('POINT(30.424 -1.978)', 4326))
FROM (SELECT rast FROM mytable) sq(rast);
like image 191
Craig Ringer Avatar answered Mar 21 '26 09:03

Craig Ringer



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!