[postgresql-it] R: Re: Eliminazione duplicati in tabella

  • From: "acheriom@xxxxxxxxx" <acheriom@xxxxxxxxx>
  • To: <postgresql-it@xxxxxxxxxxxxx>
  • Date: Sun, 25 Jan 2015 22:36:17 +0100 (CET)

Grazie per l'aiuto, Matteo. Mi ero dimenticato dell'argomento EXISTS.

Alberto




----Messaggio originale----
Da: php@xxxxxxxxxxx
Data: 25/01/2015 20.15
A: <postgresql-it@xxxxxxxxxxxxx>
Ogg: Re: [postgresql-it] Eliminazione duplicati in tabella

On 25/01/2015 18:31, acheriom@xxxxxxxxx wrote:
Ciao a tutti,

Ho la seguente tabella

id    current_step    customer_id
60978    2    1002
60875    1    1002
60894    1    1014
60862    3    1014
60921    1    1057
60864    1    1059
60872    1    1073

Vorrei fare in modo che lo stesso customer_id appaia una sola volta
cancellando le righe dove il valore di current_step non sia quello piu'
alto nel gruppo customer_id. Nell'esempio sopra vorrei cancellare le
righe con id 60875 e 60894.

Classico problema da manuale ;)

regression=# SELECT * FROM tbl t1 WHERE EXISTS (SELECT * FROM tbl t2 
WHERE t1.customer_id = t2.customer_id AND t2.current_step > 
t1.current_step);

  id   | current_step | customer_id
-------+--------------+-------------
 60875 |            1 |        1002
 60894 |            1 |        1014
(2 rows)

Ovviamente basta usare DELETE anziché SELECT *


Cheers
-- 
Matteo Beccati

Development & Consulting - http://www.beccati.com/
_______________________________________________
Postgresql-it mailing list
Postgresql-it@xxxxxxxxxxxxx
http://lists.psql.it/mailman/listinfo/postgresql-it




Other related posts:

  • » [postgresql-it] R: Re: Eliminazione duplicati in tabella - acheriom@xxxxxxxxx