Received: from localhost (unknown [200.46.208.211]) by mail.postgresql.org (Postfix) with ESMTP id B105163395F for ; Thu, 14 May 2009 11:42:36 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by localhost (mx1.hub.org [200.46.208.211]) (amavisd-maia, port 10024) with ESMTP id 98311-08 for ; Thu, 14 May 2009 11:42:24 -0300 (ADT) X-Greylist: delayed 01:14:49.815309 by SQLgrey-1.7.6 Received: from mail.ringways.co.uk (hub.ringways.co.uk [80.193.83.130]) by mail.postgresql.org (Postfix) with ESMTP id DC2E2633890 for ; Thu, 14 May 2009 11:42:33 -0300 (ADT) Received: from eddie.ringways.co.uk ([10.1.1.115]) by mail.ringways.co.uk with esmtp (Exim 4.69) (envelope-from ) id 1M4avE-0002Jl-4F for pgsql-sql@postgresql.org; Thu, 14 May 2009 14:24:30 +0100 From: Gary Stainburn Organization: Ringways Garages Ltd To: pgsql-sql@postgresql.org Subject: update from join Date: Thu, 14 May 2009 14:27:14 +0100 User-Agent: KMail/1.9.10 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200905141427.15022.gary.stainburn@ringways.co.uk> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0 tagged_above=0 required=5 tests=none X-Spam-Level: X-Archive-Number: 200905/67 X-Sequence-Number: 32559 I know I should be able to do this but my brain's mashed today I have a stock table with s_stock_no varchar primary key s_vin varchar s_updated timestamp s_superceded boolean It is possible for the same vin to exist on stock if we have sold and then bought back a vehicle, e.g. as a part exchange. Every time a vehicle is inserted/updated the s_updated field is update. How can I update the table so that for each s_vin, if a record does not have the most recent s_updated value, s_superceded is set to true? I can get the most recent value by running: select * from (select s_vin, count(s_updated) as numb, max(s_updated)::timestamp as latest from stock group by s_vin) foo where numb > 1; but I can't seem to get how I can convert this to an update statement. The num > 1 simply removed all vehicles with only one record. I seem to think I need an update..... from..... statement -- Gary Stainburn This email does not contain private or confidential material as it may be snooped on by interested government parties for unknown and undisclosed purposes - Regulation of Investigatory Powers Act, 2000