Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8542099
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:58:57+00:00 2026-06-11T11:58:57+00:00

I have a PostGIS query where I really need to have nested queries inside

  • 0

I have a PostGIS query where I really need to have nested queries inside PostGIS function calls:

UPDATE raw.geocoding
SET the_geom = ST_Centroid(
   ST_Collect(
     SELECT the_geom
     FROM raw.geocoding
     WHERE hash = ((E'0101000020090C000081610F9CC5DC3341EE672E6E723B3241')::varchar),
     SELECT the_geom
     FROM raw.geocoding
     WHERE hash = ((E'0101000020090C00002CF887E0C5DC3341C9E5B2DF2A383241')::varchar)
    )
  )
WHERE hash = ((E'3e638a27c6c38f05026252f4a0b57b2e')::varchar)

Unfortunately, this doesn’t work. I get a syntax error at the beginning of the nested query:

ERROR:  syntax error at or near "SELECT"
LINE 4:          SELECT the_geom
                 ^

********** Error **********

ERROR: syntax error at or near "SELECT"
SQL state: 42601
Character: 86

Looks like I cannot have a nested query as a PostGIS function parameter?

I’ve perused through the PostGIS documentation and cannot find any clear guidance for dealing with this.

It appears Postgres has a way of doing variables in pgSQL, but it’s unclear to me how this would be pulled off in a standard query. This is a query that will be run tens or hundreds of thousands of times from a C# program. That aside, I could do a pgSQL stored procedure if required; just wanted to make sure there wasn’t a simpler alternative first.

In case you were wondering, the query looks messy because it’s the result of a npgsql-generated parameterized query. I think it’s fair to say that npgsql is being extra-cautious with redundant typing and escaping.

I am running PostGIS 2.0.1, Postgres 9.1.5, and npgsql 2.0.12.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T11:58:59+00:00Added an answer on June 11, 2026 at 11:58 am

    It sounds like you want a scalar subquery, an expression written like (SELECT ....) (note enclosing parentheses) that contains a query returning either zero rows (NULL result) or one field from one row.

    You were most of the way there, you just needed the parens:

    UPDATE raw.geocoding
    SET the_geom = ST_Centroid(
       ST_Collect(
         (SELECT the_geom
         FROM raw.geocoding
         WHERE hash = ((E'0101000020090C000081610F9CC5DC3341EE672E6E723B3241')::varchar)),
         (SELECT the_geom
         FROM raw.geocoding
         WHERE hash = ((E'0101000020090C00002CF887E0C5DC3341C9E5B2DF2A383241')::varchar))
        )
      )
    WHERE hash = ((E'3e638a27c6c38f05026252f4a0b57b2e')::varchar)
    

    Note that subqueries can be used in other places too – table returning subqueries can appear in FROM, for example. The PostgreSQL manual teaches about all this, and is well worth a cover-to-cover read.

    If you’re doing a lot of these updates, you may find it more efficient to formulate the UPDATE as a join using the PostgreSQL extension UPDATE ... FROM ... WHERE rather than running lots of individual UPDATEs over and over. I just wanted to raise the possibility. See from-list in UPDATE

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Postgres/PostGIS query: UPDATE raw.geocoding SET the_geom = ST_Transform(ST_GeomFromText('POINT(((E'-96.6864379495382')::float8) ((E'32.792527154088')::float8))', 4326),3081) WHERE
I have this parameterized query in an Npgsqlcommand: UPDATE raw.geocoding SET the_geom = ST_Transform(ST_GeomFromText('POINT(:longitude
I'm using Rails 3.1, and I have tables with the PostGIS geometry datatype. These
I have the lat/lon of 2 points on the earth. They are really close
I have a PostgreSQL\PostGIS spatial database which contains Hebrew text columns. The system runs
EDIT: I have been using Postgres with PostGIS for a few months now, and
I'm using postgres (postgis) and i want to perform a query that returns all
I have a PostgreSQL database with PostGIS functions loaded into it. I would like
I have a database with an old broken version of PostGIS installed in it.
I am new to PostGIS and need to ask for some help here. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.