I have three CTEs and I want to update two columns in the last CTE, but I get an error that "the relation does not exist". I am using the CTEs, because, I want to store the whole process in a materialised view future use. I know there are many examples on the internet and several questions here, but I could not get it to work.
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;