pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: paul.malm@lfv.se
To: mlewis@entrata.com
Cc: pgsql-general@lists.postgresql.org
Subject: SV: SQL delete and update at the same time
Date: Thu, 25 Jun 2020 12:05:27 +0000
Message-ID: <e2ab484175954c4b82c1fac4f9c27d1f@lfv.se> (raw)
In-Reply-To: <CAHOFxGqgZHZVKKWET1ifMNW24QEbX2zyb9yQDQJk0W266nKmow@mail.gmail.com>
References: <9F4A5D06-1815-4723-9189-99F4C5067C53@lfv.se>
	<CAHOFxGqgZHZVKKWET1ifMNW24QEbX2zyb9yQDQJk0W266nKmow@mail.gmail.com>

Hi Michael,
Thanks for taking interest in my problem.
My biggest problem is that I’m not so familiar with SQL, and having problems to see how I can realize your suggestion.
I’ve been testing in pgAdmin with 2 tables:
linesegments and buffered.

linesegments (splitted line strings into segments)
fid (integer,  the same value for all line segments from the original line)
the_geom (Postgis geometry LineString)
edited, integer set to 0 (afterwards, 1 = shall be deleted, 2 = not to be deleted)
gid (integer, PRIMARY key)

buffered (polygons from bluffering original lines, thin buffer
fid (integer, the same value as the original line strings)
the_geom (Postgis geometry Polygon)
gid (integer, PRIMARY key)

This is what I’ve tried in pgAdmin4 from reading your comment
UPDATE linesegments l
set edited = CASE
    WHEN l.gid IN (SELECT li.gid FROM linesegments li, buffered as b WHERE ST_Contains(b.the_geom, li.the_geom) AND
                                            (l.edited = 0 OR l.edited = null) AND
                                            NOT b.fid=l.fid) THEN
    WHEN l.gid IN (SELECT li.gid FROM linesegments li, buffered as b WHERE ST_Contains(b.the_geom, li.the_geom) AND
                                            (l.edited = 0 OR l.edited = null) AND
                                            b.fid=l.fid) THEN 2
END

But the line segments that are within several buffer-polygon is set to 1 and the line segments that are  within only one buffer polygon is set to 2. I thought this SQL-command would at least have on segment line set to 2 where there are duplicates. And it is very slow, although I created gist index on the two tables.
Can you see what I’m doing wrong?

Kind regards,
Paul
Från: Michael Lewis [mailto:mlewis@entrata.com]
Skickat: den 24 juni 2020 21:33
Till: Malm, Paul (Operations AIM)
Kopia: PostgreSQL General
Ämne: Re: SQL delete and update at the same time

But how can I set the edited vale = 1 on the objects (line segments) that are not deleted (in the current buffer) at the same time so it won’t be deleted in the next run with an adjacent buffer?

You might want to create a temporary table to hold unique identifiers of all records that you want to either delete or edit, and make use of a RETURNING clause to determine which got deleted, then update the records NOT IN that returning clause values (or use EXCEPT). Perhaps that would get you where you want to end up.


Message-ID: <e2ab484175954c4b82c1fac4f9c27d1f@lfv.se>
Permalink:  ../e2ab484175954c4b82c1fac4f9c27d1f@lfv.se/
Also on:    postgresql.org/message-id/e2ab484175954c4b82c1fac4f9c27d1f@lfv.se

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: paul.malm@lfv.se, mlewis@entrata.com
  Subject: Re: SQL delete and update at the same time
  In-Reply-To: <e2ab484175954c4b82c1fac4f9c27d1f@lfv.se>
  References: <9F4A5D06-1815-4723-9189-99F4C5067C53@lfv.se> <CAHOFxGqgZHZVKKWET1ifMNW24QEbX2zyb9yQDQJk0W266nKmow@mail.gmail.com> <e2ab484175954c4b82c1fac4f9c27d1f@lfv.se>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox