Re: SQL for Deleting all duplicate entries

Поиск
Список
Период
Сортировка
От Scott Marlowe
Тема Re: SQL for Deleting all duplicate entries
Дата
Msg-id dcc563d10709050755v7ff08941n49d4ed73ae1ac312@mail.gmail.com
обсуждение исходный текст
Ответ на SQL for Deleting all duplicate entries  (Håkan Jacobsson <hakan.jacobsson99@bredband.net>)
Ответы Re: SQL for Deleting all duplicate entries  ("Scott Marlowe" <scott.marlowe@gmail.com>)
Список pgsql-general
On 9/5/07, Håkan Jacobsson <hakan.jacobsson99@bredband.net> wrote:
> Hi,
>
> I want to create a DELETE statement which deletes duplicates
> in a table.
>
> That is, I want to remove all rows - but one - having three
> columns with the same data (more columns exist and there the
> data varies).

Assuming you've got a KNOWN unique id field (adding one if you don't)
you can do something like:

select * from table t1 join table t2 on (t1.field1=t2.field1 AND
t1.field2=t2.field2 AND t1.field3=t2.field3 AND t1.uid>t2.uid)

That should get the ids of all but one of the matching rows.  then
just use that in a subselect:

begin;
delete from table where uid in (select * from table t1 join table t2
on (t1.field1=t2.field1 AND t1.field2=t2.field2 AND
t1.field3=t2.field3 AND t1.uid>t2.uid) );
(check for dups / lost data)
commit;
or something like that.

В списке pgsql-general по дате отправления:

Предыдущее
От: "Scott Marlowe"
Дата:
Сообщение: Re: SQL query with IFs (?) to "Eliminate" NULL Values
Следующее
От: Alvaro Herrera
Дата:
Сообщение: Re: problem with transactions in VB.NET using npgsql