Обсуждение: delete non-unique

Поиск
Список
Период
Сортировка

delete non-unique

От
henlin
Дата:
hello.


i need to clean up some tables: from several equal rows keep only one
(like `sort -u`)

aaa bbb - do not delete
aaa bbb - delete
aaa bbb - delete


fenx
--
Отправлено M2, революционной почтовой программой Opera:
http://www.opera.com/mail/mail/

Re: delete non-unique

От
"A. Kretschmer"
Дата:
am  Wed, dem 25.04.2007, um 13:08:15 +0400 mailte henlin folgendes:
> hello.
>
>
> i need to clean up some tables: from several equal rows keep only one
> (like `sort -u`)

You can create a new table as select distinct from the original table
and rename both tables properly.

Other solution, i demonstrate with an example:

test=*# select ctid,* from doubles ;
 ctid  | id | name
-------+----+------
 (0,1) |  1 | foo
 (0,2) |  1 | foo
 (0,3) |  2 | bar
 (0,4) |  2 | bar
 (0,5) |  2 | bar
 (0,6) |  3 | batz
 (0,7) |  3 | batz
 (0,8) |  3 | batz
(8 rows)


test=*# delete from doubles where ctid not in (select distinct on (id, name) ctid from doubles );
DELETE 5
test=*# select ctid,* from doubles ;
 ctid  | id | name
-------+----+------
 (0,1) |  1 | foo
 (0,3) |  2 | bar
 (0,6) |  3 | batz
(3 rows)



The ctid-column is a hidden column with an unique value within this
table.


Andreas
--
Andreas Kretschmer
Kontakt:  Heynitz: 035242/47150,   D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID:   0x3FFF606C, privat 0x7F4584DA   http://wwwkeys.de.pgp.net

Re: delete non-unique

От
Phillip Smith
Дата:
Similar to other suggestion:

BEGIN;
SELECT DISTINCT ON (col1) * INTO TEMP unique_rows FROM table1;
COPY unique_rows TO '/tmp/unique.sql';
TRUNCATE TABLE table1;
COPY table1 FROM '/tmp/unique.sql';
COMMIT;

On Wed, 2007-04-25 at 13:08 +0400, henlin wrote:
i need to clean up some tables: from several equal rows keep only one  
(like `sort -u`)

aaa bbb - do not delete
aaa bbb - delete
aaa bbb - delete

*******************Confidentiality and Privilege Notice*******************

The material contained in this message is privileged and confidential to the addressee. If you are not the addressee indicated in this message or responsible for delivery of the message to such person, you may not copy or deliver this message to anyone, and you should destroy it and kindly notify the sender by reply email.

Information in this message that does not relate to the official business of Weatherbeeta must be treated as neither given nor endorsed by Weatherbeeta. Weatherbeeta, its employees, contractors or associates shall not be liable for direct, indirect or consequential loss arising from transmission of this message or any attachments