我正在尝试使用POSTGRESQL中的WITH语句进行以下查询:
WITH catchment AS (
SELECT * FROM .. -- Some time consuming join statement
),
catchment10 AS (
SELECT * FROM pgr_pointsaspolygon('SELECT * FROM catchment where cost < 10000')
),
catchment20 AS (
SELECT * FROM pgr_pointsaspolygon('SELECT * FROM catchment where cost < 20000')
),
catchment30 AS (
SELECT * FROM pgr_pointsaspolygon('SELECT * FROM catchment where cost < 30000')
)
SELECT * FROM catchment10
UNION
SELECT * FROM catchment20
UNION
SELECT * FROM catchment30
问题是pgr_pointsaspolygon()函数调用了pgr_alphashape()函数,该函数使用了一些c库:
CREATE OR REPLACE FUNCTION pgr_alphashape(IN sql text, OUT x double precision, OUT y double precision)
RETURNS SETOF record AS
'$libdir/librouting_dd', 'alphashape'
LANGUAGE c IMMUTABLE STRICT
COST 1
ROWS 1000;
ALTER FUNCTION pgr_alphashape(text)
OWNER TO postgres;
pgr_alphashape需要一个sql语句才能工作,但是当我从WITH子句传递catchment表时,它会出现以下错误:
ERROR: relation "catchment" does not exist
是否有任何其他方法或方法来纠正此错误,以便集水表不会被计算3次,但可以传递给pgr_pointsaspolygon函数?
答案 0 :(得分:2)
由于您显然正在使用第三方提供的功能,因此您唯一的选择是临时表。将CREATE TABLE AS
与ON COMMIT DROP
一起使用可避免与同一会话中的多个呼叫冲突。
BEGIN;
CREATE TEMP TABLE catchment ON COMMIT DROP AS
SELECT * FROM .. -- Some time consuming join statement
;
SELECT * FROM pgr_pointsaspolygon('SELECT * FROM catchment WHERE cost < 10000')
UNION ALL -- Presuming we do not want to eliminate dupes?
SELECT * FROM pgr_pointsaspolygon('SELECT * FROM catchment WHERE cost < 20000')
UNION ALL
SELECT * FROM pgr_pointsaspolygon('SELECT * FROM catchment WHERE cost < 30000')
COMMIT;