如何在PostgreSQL中循环遍历行?

时间:2014-12-14 18:44:03

标签: postgresql loops rows postgis

我有两张表:test_linetest_vertex

此外,我还有以下查询,该查询根据test_line选择test_vertex的部分内容。如果我的两个表包含一行,查询工作正常。现在我想将查询用于多行。是否有一种简单的方法可以对行进行两次循环并应用查询,而不会对原始查询进行太多改动?

 WITH
 lines as (SELECT wkb_geometry from test_line as geom),
 points as (SELECT geom from test_vertex as geom),
 numgeoms (pt_count) as (SELECT st_numgeometries(geom) + 1 as pt_count FROM points),
 knife_points as (
   SELECT x as segment, CASE WHEN x = 0 then 0
     WHEN x = (select pt_count from numgeoms) THEN 1 ELSE
     ST_Line_Locate_Point(l.wkb_geometry, ST_GeometryN(p.geom,x)) END as line_fraction_end,
     CASE WHEN x = 1 THEN 0  else
     ST_Line_Locate_Point(l.wkb_geometry, ST_GeometryN(p.geom,x-1)) END as line_fraction_start
   FROM points p, lines l, (SELECT generate_series(0, (SELECT pt_count from numgeoms)) as x ) g),
   segments as
    (SELECT ST_Line_Substring(wkb_geometry, line_fraction_start, line_fraction_end) as geom, segment
     FROM knife_points, lines WHERE segment >0 )
   SELECT geom, segment from segments
   WHERE ST_GeometryType(geom) = 'ST_LineString';

0 个答案:

没有答案