X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 53AA8D1B4F0 for ; Tue, 23 Sep 2003 03:31:21 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 43708-04 for ; Tue, 23 Sep 2003 00:31:10 -0300 (ADT) Received: from news.hub.org (unknown [64.117.224.194]) by svr1.postgresql.org (Postfix) with ESMTP id BD89CD1B4FF for ; Tue, 23 Sep 2003 00:31:10 -0300 (ADT) Received: from news.hub.org (host-64-117-224-194.altec1.com [64.117.224.194] (may be forged)) by news.hub.org (8.12.9/8.12.9) with ESMTP id h8N3VAhK031159 for ; Tue, 23 Sep 2003 03:31:10 GMT (envelope-from news@news.hub.org) Received: (from news@localhost) by news.hub.org (8.12.9/8.12.9/Submit) id h8N3KLpt029891 for pgsql-sql@postgresql.org; Tue, 23 Sep 2003 03:20:21 GMT From: Christopher Browne X-Newsgroups: comp.databases.postgresql.sql Subject: Re: selecting duplicate records Date: Mon, 22 Sep 2003 23:13:23 -0400 Organization: cbbrowne Computing Inc Lines: 43 Message-ID: References: <3F6EF83A.129807D6@rodos.fzk.de> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Complaints-To: usenet@news.hub.org X-message-flag: Outlook is rather hackable, isn't it? X-Home-Page: http://www.cbbrowne.com/info/ X-Affero: http://svcs.affero.net/rm.php?r=cbbrowne User-Agent: Gnus/5.1003 (Gnus v5.10.3) XEmacs/21.4 (Rational FORTRAN, linux) Cancel-Lock: sha1:r5vHxtjQnkZLd3z0YwzxV+QwrRw= To: pgsql-sql@postgresql.org X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/266 X-Sequence-Number: 15244 The world rejoiced as ch@rodos.fzk.de (Christoph Haller) wrote: >> 1. How to select duplicate records only from a single table using a > select >> query. >> > e.g. > select sid,count(sid) from location group by sid having count(sid)>1; > > Do you get the idea? > Your request is pretty unspecific, so if this is not what you're asking > for, > try again. The aggregate is likely to perform horrifically badly. Here might be an option: Step 1. Find all of the duplicates... select a.* into temp table sid from some_table a, some_table b where a.oid < b.oid and a.field1 = b.field1 and a.field2 = b.field2 and a.field3 = b.field3 and ... a.fieldn = b.fieldn; Step 2. Look for the matching entries in the source table... select a.* from some_table a, sid b where a.field1 = b.field1 and a.field2 = b.field2 and a.field3 = b.field3 and ... a.fieldn = b.fieldn; [There's a weakness here; if there are multiple dupes, they may get picked multiple times in the second query :-(.] -- If this was helpful, rate me http://cbbrowne.com/info/rdbms.html As Will Rogers would have said, "There is no such thing as a free variable." -- Alan Perlis