X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.193]) by svr1.postgresql.org (Postfix) with ESMTP id C41C930F290 for ; Sun, 22 Jun 2003 11:03:49 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (svr1.postgresql.org [64.117.224.193]) (amavisd-new, port 10024) with ESMTP id 91566-10 for ; Sun, 22 Jun 2003 08:03:39 -0300 (ADT) Received: from curry.tmsl.demon.co.uk (tmsl-adsl.demon.co.uk [80.177.114.181]) by svr1.postgresql.org (Postfix) with ESMTP id 5F3EA30EF90 for ; Sun, 22 Jun 2003 08:03:38 -0300 (ADT) Received: from bacon.tmsl.demon.co.uk (bacon.tmsl.demon.co.uk [192.168.7.102]) by curry.tmsl.demon.co.uk (8.11.6/8.11.6) with ESMTP id h5MB3cl05472 for ; Sun, 22 Jun 2003 12:03:38 +0100 Date: Sun, 22 Jun 2003 12:03:35 +0100 From: Paul Thomas To: "pgsql-sql @ postgresql . org" Subject: Re: Delete duplicates Message-ID: <20030622120335.C4067@bacon> References: <20030622091522.M37248@oasis.net.au> Mime-Version: 1.0 Content-Type: text/plain; format=flowed; charset=ISO-8859-1 In-Reply-To: <20030622091522.M37248@oasis.net.au>; from rudi@oasis.net.au on Sun, Jun 22, 2003 at 10:15:22 +0100 X-Mailer: Balsa 1.2.3 Lines: 58 X-Archive-Number: 200306/242 X-Sequence-Number: 14000 On 22/06/2003 10:15 Rudi Starcevic wrote: > > > Hi, > > I have a table with duplicates and trouble with my SQL. > I'd like to keep a single record and remove older duplicates. > For example below of the 6 recods I'd like to keep records > 4 and 6. > > TABLE: aap > id | keyword > ----+----------------- > 1 | LEAGUE PANTHERS > 2 | LEAGUE PANTHERS > 3 | LEAGUE PANTHERS > 4 | LEAGUE PANTHERS > 5 | LEAGUE BRONCOS > 6 | LEAGUE BRONCOS > > Here is my SQL so far, it will select records 1 to 5 instead > of 1,2,3 and 5 only. > > Any help greatly appreciated. I think I need a Group By somewhere in > there. > > select a1.id > from aap a1 > where id < ( SELECT max(id) FROM aap AS a2 ) > AND EXISTS > ( > SELECT * > FROM aap AS a2 > WHERE a1.keyword = a2.keyword > ) I just tries this with 7.3.3: select max(id), keyword from aap where keyword in (select distinct keyword from aap) group by keyword; max | keyword ----------------------- 6 | LEAGUE BRONCOS 4 | LEAGUE PANTHERS (2 rows) HTH -- Paul Thomas +------------------------------+---------------------------------------------+ | Thomas Micro Systems Limited | Software Solutions for the Smaller Business | | Computer Consultants | http://www.thomas-micro-systems-ltd.co.uk | +------------------------------+---------------------------------------------+