显示选择查询的结果

时间:2017-10-13 08:50:10

标签: postgresql

您是否知道如何显示以下查询的结果或将其存储在表格中:

do
 $$
declare 
 i record;
begin

  for i in 1..3 loop
   PERFORM ROW_NUMBER() OVER() AS id,
    sub_query.*
   FROM line AS b, LATERAL (
   SELECT array_agg(DISTINCT a.id) AS old_id,
       ST_Union(a.geom) AS fusion
   FROM line AS a
   WHERE a.id > b.id
   AND ST_touches(a.geom, b.geom) = true 
   AND a.material = b.material

   GROUP BY a.material

   ) AS sub_query
   ;
  end loop;
  end;
  $$
  ;

1 个答案:

答案 0 :(得分:0)

我没有看到你如何在上面的代码中使用记录,所以只运行三次:

SELECT ROW_NUMBER() OVER() AS id,
    sub_query.*
   FROM line AS b, LATERAL (
   SELECT array_agg(DISTINCT a.id) AS old_id,
       ST_Union(a.geom) AS fusion
   FROM line AS a
   WHERE a.id > b.id
   AND ST_touches(a.geom, b.geom) = true 
   AND a.material = b.material

   GROUP BY a.material

   ) AS sub_query
   ;

?..

如果您在理论上提出要求,请使用raise输出值,或使用insert into将它们放到表中......