I've got a PL/pgSQL function that return a table whose definition is as follows:
CREATE OR REPLACE FUNCTION get_movement_aggregation(movement_table varchar,place_table varchar,geom varchar,point_gid varchar, radius numeric DEFAULT 20000, inArea geometry DEFAULT NULL)
RETURNS TABLE(place_i integer) AS
-- actual code here
Now I would like to use this function by using a geometry (the inArea
parameter) that I get from another table. When I try the following query:
WITH x AS (
SELECT wkb_geometry from tha_adm1
WHERE name_1 = 'Ubon Ratchathani' LIMIT 1
)
SELECT g.*
FROM x,
get_movement_aggregation('movements','places','geom','place_id',14000,x.wkb_geometry)
I get the following error:
ERROR: function expression in FROM cannot refer to other relations of same query level
It does make sense as I indeed refer to x
in both parts of the FROM
clause.
The subquery returns the following:
+----------------------------------+
| | wkb_geometry |
| | geometry(MultiPolygon,4326) |
+---+------------------------------+
| 1 | some very long number here |
+---+------------------------------+
And I just use this parameter to filter the list of records I am working with in the function. One solution would be to create a temporary table with the filtered records and feed to the function but if there is a way to do that with a subquery, that would be great.
Thank you in advance for your answers.