I have a PostgreSQL table containing some point of interest data. I have a second table with overlapping continent polygons. The following code splits the POI into the continents, but instead of extracting them, the continent ID gets written into a column of the POI data. The query is working so far.
select t3.*, array_to_string(array_cont, '') as conts from
(select ogc_fid, array_agg(conts) as array_cont from
(SELECT t1.*, cont.id AS conts
FROM continents cont, poi t1
WHERE st_contains(cont.geom, t1.wkb_geometry)
order by conts)t1
group by ogc_fid)t2, poi t3
where t2.ogc_fid = t3.ogc_fid
order by conts;
But when I run this query inside a CREATE VIEW statement, I get the following error:
CREATE OR REPLACE VIEW poi_split AS
select t3.*, array_to_string(array_cont, '') as conts from
(select ogc_fid, array_agg(conts) as array_cont from
(SELECT t1.*, cont.id AS conts
FROM continents cont, poi t1
WHERE st_contains(cont.geom, t1.wkb_geometry)
order by conts)t1
group by ogc_fid)t2, poi t3
where t2.ogc_fid = t3.ogc_fid
order by conts;
ERROR: syntax error at or near "create"
LINE 1: create or replace view poi_split as
^
Sorry for the bad formatting, but as far as I can see, it is no syntax error involved, as the query runs perfect without CREATE VIEW AS. It's even possible to run
CREATE OR REPLACE VIEW poi_split AS SELECT * FROM poi;
What am I doing wrong?
psql -f
to load the definition from a file, pasting the code, reading it from stdin, ... ?