2

I have a table in a PostGIS database where with this query :

SELECT ST_CENTROID(pol8.geom) AS geom
FROM polygon8 AS pol8;

I got this:

                        geom                        
----------------------------------------------------
 0101000020647F00001AFAEAE428C91641E38374F8A8015141
 0101000020647F0000CB093A0DFA6C19416AFE29C6F8FB5041
 0101000020647F00002D325E7A35301C411F69C65A03F95041
 0101000020647F00007F7DBD9F049F1A419FDBBA0177E65041
 0101000020647F00005F7B5395D9CC18416E2BB23D61E35041
 0101000020647F000057844F356A0016417EA357E1A5EE5041
 0101000020647F0000CAFFD8471C1A18414B9D4A488DF95041
 0101000020647F000033426EF4B7B91C41C5D5530680E55041
(8 rows)

However, with this one I have a syntax error:

SELECT ST_AsText (ST_CENTROID(pol8.geom) AS geom
FROM polygon8 AS pol8);
ERROR:  syntax error at or near "AS"
LINE 1: SELECT ST_AsText (ST_CENTROID(pol8.geom) AS geom
                                                 ^

I only get the expected result with these additional queries (three in total):

CREATE TABLE centroids_pol8 AS (SELECT ST_CENTROID(pol8.geom) AS geom
FROM polygon8 AS pol8);

SELECT ST_AsText (geom) AS wkt 
FROM centroids_pol8;

                   wkt                    
------------------------------------------
 POINT(373322.223552616 4458147.88211152)
 POINT(416574.512916711 4452323.0963131)
 POINT(461837.369499954 4449293.41836002)
 POINT(436161.155996285 4430300.0270299)
 POINT(406326.395826271 4427140.96399961)
 POINT(360474.552061146 4438679.52097404)
 POINT(394887.070163724 4449845.1295541)
 POINT(470637.988701853 4429312.09886688)
(8 rows)

I tried out many syntax forms and I can not find out the solution for a query in only one path. Where is the issue in my second approach?

5

It looks like

SELECT ST_AsText (ST_CENTROID(pol8.geom) AS geom

should be

SELECT ST_AsText (ST_CENTROID(pol8.geom)) AS geom

note the second ) - when in doubt count from left to right +1 for each ( and -1 for each ) if you don't reach 0 at the end then you have missed a bracket.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.