Re: How to remove duplicate lines but save one of the lines?

From: "Hoover, Jeffrey" <jhoover(at)jcvi(dot)org>
To: "A B" <gentosaker(at)gmail(dot)com>, <pgsql-general(at)postgresql(dot)org>
Subject: Re: How to remove duplicate lines but save one of the lines?
Date: 2008-07-21 16:14:48
Message-ID: E92C2B1CB12A7A4683697273BD5DCCE402DF0A6A@EXCHANGE.TIGR.ORG
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

minor refinement on suggestion:

-- CTAS (create table as) is easiest way to create table with same
structure
create table foo as select * from orig_table;
-- truncate is much more efficient than delete
truncate orig_table;
-- unchanged
insert into orig_table select * from foo;
-- recompute statistics
analyze orig_table

-----Original Message-----
From: pgsql-general-owner(at)postgresql(dot)org
[mailto:pgsql-general-owner(at)postgresql(dot)org] On Behalf Of A B
Sent: Monday, July 21, 2008 11:51 AM
To: pgsql-general(at)postgresql(dot)org
Subject: Re: [GENERAL] How to remove duplicate lines but save one of the
lines?

> There is probably a more elegant way of doing it, but a simple way of
doing
> it ( depending on the size of the table ) could be:
>
> begin;
>
> insert into foo select distinct * from orig_table;
> delete from orig_table;
> insert into orig_table select * from foo;
>
> commit;

Just to make it clear to me
Here foo is a table that I have to create with the command
CREATE TABLE foo (....same columns as orig_table);
?

Is it possible to add a unique constraint to the table, with a
"delete" option so it will delete duplicates?

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2008-07-21 16:29:10 Re: How to remove duplicate lines but save one of the lines?
Previous Message Said Ramirez 2008-07-21 16:14:12 Re: How to remove duplicate lines but save one of the lines?