如何对整个 table 应用递归查询?

How to apply a recursive query to whole table?

此 post 与 another question of mine 有关。我想出了一个基本上想要我想要的递归查询。只要 dist_calc_points 属性的计数没有超过,就会执行递归查询。但这仅适用于一个条目(请参阅 WHERE v2_channel.id=2 子句)。如何将此查询应用于整个 table?

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;