Received: from bologna.nettuno.it (bologna.nettuno.it [193.43.2.1]) by hub.org (8.9.3/8.9.1) with ESMTP id JAA12108 for ; Mon, 26 Apr 1999 09:01:10 -0400 (EDT) (envelope-from jose@sferacarta.com) Received: from proxy.sferacarta.com (mail@sfcabop1.nettuno.it [193.207.10.213]) by bologna.nettuno.it (8.8.6/8.8.6/NETTuno 3.1) with ESMTP id PAA00024; Mon, 26 Apr 1999 15:00:48 +0200 (MDT) Received: from rosso.sferacarta.com (sferacarta.com) [10.20.30.5] by proxy.sferacarta.com with esmtp (Exim 2.05 #1 (Debian)) id 10bmcE-0003sD-00; Mon, 26 Apr 1999 14:44:10 +0000 Message-ID: <37245F9D.F0232CC2@sferacarta.com> Date: Mon, 26 Apr 1999 14:44:14 +0200 From: =?iso-8859-1?Q?Jos=E9?= Soares X-Mailer: Mozilla 4.5 [it] (Win95; I) X-Accept-Language: it MIME-Version: 1.0 To: Tim Perdue CC: pgsql-sql@hub.org Subject: Re: [SQL] Primary Key Bugs References: <000c01be8f45$6c334f90$0b8c5aa5@timnt.weather.net> Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Tim Perdue ha scritto: > Somehow I have records in my database with duplicate primary keys. Because > of this, I'm not able to update a lot of records, because Postgres then > complains that I'm trying to insert a duplicate primary key. > > Can anyone suggest a way to select the duplicate ids out of the table so I > can change them? > > I can't really do a SELECT DISTINCT on a 3GB database table or my machine > would likely go down. > > Basically, I just want a query like this: > > select * into tbl_tmp from tbl_mail where mailid is duplicated > > 8-) > > Any suggestions? > > Tim Perdue > PHPBuilder.com / GotoCity.com / Geocrawler.com The SQL expression to retrieve duplicate rows is: select * from tbl_mail where mailid in ( select mailid from tbl_mail group by mailid having count(mailid) > 1 ); But currently PostgreSQL have problems with having (see TODO): * subqueries containing HAVING return incorrect results Therefore you have to query the tbl_mail in two times. 1) select mailid from tbl_mail group by mailid having count(mailid) > 1 2) select * from tbl_mail where mailid = (replace with values returned at point 1) José