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 B31CAD1B58D for ; Tue, 23 Sep 2003 20:34:43 +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 46274-04 for ; Tue, 23 Sep 2003 17:34:34 -0300 (ADT) Received: from bach.ccinet.ab.ca (bach.ccinet.ab.ca [198.161.96.1]) by svr1.postgresql.org (Postfix) with ESMTP id E94BDD1B519 for ; Tue, 23 Sep 2003 17:34:31 -0300 (ADT) Received: from incentre.net (guy-pc.incentre.net [206.75.213.195] (may be forged)) by bach.ccinet.ab.ca (8.12.8p2/8.12.8) with ESMTP id h8NKXYaj023705 for ; Tue, 23 Sep 2003 14:33:34 -0600 (MDT) (envelope-from guy@incentre.net) Message-ID: <3F70AE58.7030308@incentre.net> Date: Tue, 23 Sep 2003 14:34:32 -0600 From: Guy Fraser Organization: The Internet Centre User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.2.1) Gecko/20030225 X-Accept-Language: en-ca, en-gb, en-us, en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: selecting duplicate records References: <3F6EF83A.129807D6@rodos.fzk.de> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200309/276 X-Sequence-Number: 15254 This is a test I did on one of my tables where I put duplicated entries : select * from acct_other_2003sep except select sep.* from ( select min(oid) as min_oid, "Acct-Status-Type", "User-Name", "Realm", "Acct-Session-Time", "Acct-Input-Octets", "Acct-Output-Octets", "Called-Station-Id", "Calling-Station-Id", "Acct-Terminate-Cause", "Framed-IP-Address", "Service-Type", "Framed-Protocol", "Client-IP-Address", "NAS-IP-Address", "NAS-Port-Type", "NAS-Port-Id", "Acct-Session-Id", "Acct-Link-Count", "Acct-Multi-Session-Id" from acct_other_2003sep group by "Acct-Status-Type", "User-Name", "Realm", "Acct-Session-Time", "Acct-Input-Octets", "Acct-Output-Octets", "Called-Station-Id", "Calling-Station-Id", "Acct-Terminate-Cause", "Framed-IP-Address", "Service-Type", "Framed-Protocol", "Client-IP-Address", "NAS-IP-Address", "NAS-Port-Type", "NAS-Port-Id", "Acct-Session-Id", "Acct-Link-Count", "Acct-Multi-Session-Id" ) as min_sep, acct_other_2003sep as sep where sep.oid = min_sep.min_oid ; From the above example you can see how to use a subselect to get a unique list then using except, you can get the records that were not unique. This may not be exactly what you want but it does implement some of the methods required to get around using using temporary tables. For some tasks using temporary tables may be more suitable if your query becomes too complex and or you run out of memory/time. Hope this helps. Guy Christopher Browne wrote: >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 :-(.] > > -- Guy Fraser Network Administrator The Internet Centre 780-450-6787 , 1-888-450-6787 There is a fine line between genius and lunacy, fear not, walk the line with pride. Not all things will end up as you wanted, but you will certainly discover things the meek and timid will miss out on.