From: benj.dev@laposte.net
To: Roger Bos <roger.bos@gmail.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: delete query using CTE
Date: Sun, 13 Mar 2022 18:05:50 +0100
Message-ID: <0cc10ba0-8979-a064-0714-cc5d3d50cf7b@laposte.net> (raw)
In-Reply-To: <CAPV07m-_L6Ssi4YkiFVkej94fzOWwK2RbGCLS6SZum0WwW2PLw@mail.gmail.com>
References: <CAPV07m-_L6Ssi4YkiFVkej94fzOWwK2RbGCLS6SZum0WwW2PLw@mail.gmail.com>
Le 13/03/2022 à 15:44, Roger Bos a écrit :
> Hello, trying to use CTE to remove duplicates from a table. The DELETE
> version does not work, but the SELECT version does, so I am not
> understanding what the problem is. Any suggestions on how to fix it?
>
> Here is my query:
>
> WITH cte AS
> ( SELECT *, ROW_NUMBER() OVER ( PARTITION BY ticker, date ORDER BY
> ticker, date) my_row_num FROM price_old)
> DELETE FROM cte WHERE my_row_num > 1;
>
> I get the following error:
>
> ERROR: relation "cte" does not exist LINE 3: DELETE FROM cte WHERE
> my_row_num > 1;
>
> But when I run change the query to a select query it runs fine (in that
> it returns all the duplicate rows). For example:
>
> WITH cte AS
> ( SELECT *, ROW_NUMBER() OVER ( PARTITION BY ticker, date ORDER BY
> ticker, date) my_row_num FROM price_old)
> SELECT * FROM cte WHERE my_row_num > 1;
>
> Sample output:
>
> "US000000094541" "AAC" "2022-03-08 00:00:00-05" 9.75 9.76 9.75 9.75
> 100215 9.75 9.76 9.75 9.75 100215 0 1 2
> "US000000094541" "AAC" "2022-03-09 00:00:00-05" 9.75 9.76 9.75 9.76
> 111334 9.75 9.76 9.75 9.76 111334 0 1 2
> "US000000009823" "AAC" "2022-03-10 00:00:00-05" 9.75 9.76 9.74 9.74
> 170474 9.75 9.76 9.74 9.74 170474 0 1 2
> "US000000090393" "ABCL" "2022-03-08 00:00:00-05" 8.19 8.545 7.81 8.22
> 1984348 8.19 8.545 7.81 8.22 1984348 0 1 2
>
> Thanks,
> Roger
>
As Michael Lewis says, you can't use delete from cte but you can build
your request to do the work with something like (example from you
original request) :
WITH cte AS
( SELECT *
, ROW_NUMBER() OVER (PARTITION BY a ORDER BY b) my_row_num
, ctid -- or pk or other unique identifier if exists
FROM price_old
)
DELETE FROM tmp24
WHERE ctid IN (SELECT ctid FROM cte WHERE my_row_num = 1);
Benj
Attachments:
OpenPGP_signature (application/pgp-signature, 202B) download
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: benj.dev@laposte.net, roger.bos@gmail.com
Subject: Re: delete query using CTE
In-Reply-To: <0cc10ba0-8979-a064-0714-cc5d3d50cf7b@laposte.net>
References: <CAPV07m-_L6Ssi4YkiFVkej94fzOWwK2RbGCLS6SZum0WwW2PLw@mail.gmail.com> <0cc10ba0-8979-a064-0714-cc5d3d50cf7b@laposte.net>
* 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