这篇文章与another question of mine有关。我提出了一个基本上我想要的递归查询。只要dist_calc_points
属性的计数未超过,就会执行递归查询。但这仅适用于一个条目(请参阅WHERE v2_channel.id=2
子句)。我如何将此查询应用于整个表?
WITH RECURSIVE dist(x, the_geom, d) AS (
SELECT
0::double precision,
the_geom,
0::double precision
FROM v2_channel where v2_channel.id=2
UNION ALL
SELECT
x+1,
v2_channel.the_geom AS gm,
d+(1/v2_channel.dist_calc_points) AS dist_calc_pnts
FROM v2_channel, dist
WHERE dist.x<v2_channel.dist_calc_points AND v2_channel.id=2
)
SELECT *, ST_AsText(ST_LineInterpolatePoint(the_geom, d)) FROM dist;
答案 0 :(得分:1)
要允许CTE应用于多行,您必须能够识别这些行。所以只需添加ID:
WITH RECURSIVE dist(id, x, the_geom, d) AS (
SELECT
id,
0::double precision,
the_geom,
0::double precision
FROM v2_channel
UNION ALL
SELECT
dist.id,
x+1,
v2_channel.the_geom AS gm,
d+(1/v2_channel.dist_calc_points) AS dist_calc_pnts
FROM v2_channel JOIN dist
ON dist.x < v2_channel.dist_calc_points
AND dist.id = v2_channel.id
)
SELECT *, ST_AsText(ST_LineInterpolatePoint(the_geom, d)) FROM dist;