在 CTE 中更新列的值

Update columns' values in CTE

我有三个 CTE,我想更新最后一个 CTE 中的两列,但我收到“关系不存在”的错误消息。我正在使用 CTE,因为我想将整个过程存储在物化视图中以备将来使用。我知道网上有很多例子,这里有几个问题,但我无法让它工作。

CREATE MATERIALIZED VIEW collection.issue1 AS (
    WITH Buffer_table AS 
    (
        SELECT id, geom, ST_buffer(ST_transform(geom,2952),20) as buffer_geom 
        From locations
    )
, 
 locations_events AS 
    (
        SELECT A.id, event_id, date, field3, field4, field5, field6, field7,field8, field9
        From events.safe_copy
        CROSS JOIN LATERAL 
        (
            Select id
            FROM Buffer_table  
            WHERE ST_Within(ST_Transform(ST_SetSRID(ST_MakePoint("LONGITUDE", "LATITUDE"), 4326), 2952), buffer_geom) 
            AND date >= '2015-01-01' AND  date <='2020-12-31'
            AND field6 in ('2') 
            AND field5 in ('2')
            AND field7 in ('3', '5') 
            AND field3 in ('1','2') 
        ) a 
    )
    UPDATE locations_events SET
    field8 = CASE WHEN field8 IS NULL THEN 'No' ELSE 'Yes' END,
    field9 = CASE WHEN field9 in ('3','4') THEN 'Yes' ELSE 'No' END;

我希望这是使用 JOIN 和 ST_DWITHIN() 进行更新的良好起点。

您也可以将查询保存在 stored procedure.

注意: 这个查询没有使用空间索引,就是在大量的点上会慢慢的

UPDATE events.safe_copy sc
   SET field8 = CASE WHEN field8 IS NULL THEN 'No' ELSE 'Yes' END
     , field9 = CASE WHEN field9 in ('3','4') THEN 'Yes' ELSE 'No' END
  FROM locations loc
 WHERE ST_DWithin ( loc.geom -- geom in EPSG:4326
                  , ST_SetSRID
                             ( ST_MakePoint("LONGITUDE", "LATITUDE")
                             , 4326
                             ) -- geom in EPSG:4326
                  , 20 -- distance in meters
                  , true -- use_spheroid
                  ) 
   and sc.date >= '2015-01-01'
   and sc.date <='2020-12-31'
   and sc.field6 = '2'
   and sc.field5 = '2'
   and sc.field7 in ('3', '5') 
   and sc.field3 in ('1','2') 

如@smvenk 所述,cte 和 mat 视图不可更新。您可以将 case when 移动到 locations_events cte 中的 select 语句 :) 例如:

CREATE MATERIALIZED VIEW collection.issue1 AS (
WITH Buffer_table AS 
    (
        SELECT id, geom, ST_buffer(ST_transform(geom,2952),20) as buffer_geom 
        From locations
    )

SELECT  A.id, 
        event_id, 
        date, 
        field3, 
        field4, 
        field5, 
        field6, 
        field7,
        CASE WHEN field8 IS NULL THEN 'No' ELSE 'Yes' END as field8, 
        CASE WHEN field9 in ('3','4') THEN 'Yes' ELSE 'No' END as field9

From events.safe_copy

        CROSS JOIN LATERAL 
        (
            Select id
            FROM Buffer_table  
            WHERE ST_Within(ST_Transform(ST_SetSRID(ST_MakePoint("LONGITUDE", "LATITUDE"), 4326), 2952), buffer_geom) 
            AND date >= '2015-01-01' AND  date <='2020-12-31'
            AND field6 in ('2') 
            AND field5 in ('2')
            AND field7 in ('3', '5') 
            AND field3 in ('1','2') 
        ) a