Sql 如何对整个表应用递归查询?
这篇文章是关于。我提出了一个递归查询,它基本上是我想要的。只要dist_calc_points属性的计数没有超过,就会执行递归查询。但这只适用于一个条目,请参见WHERE v2_channel.id=2子句。如何将此查询应用于整个表Sql 如何对整个表应用递归查询?,sql,recursive-query,Sql,Recursive Query,这篇文章是关于。我提出了一个递归查询,它基本上是我想要的。只要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 F
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;
要允许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;