update from join - Mailing list pgsql-sql

From Gary Stainburn
Subject update from join
Date
Msg-id 200905141427.15022.gary.stainburn@ringways.co.uk
Whole thread Raw
Responses Re: update from join  (Rob Sargent <robjsargent@gmail.com>)
List pgsql-sql
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
groupby 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     


pgsql-sql by date:

Previous
From: Emi Lu
Date:
Subject: How to dump views definition in one schema?
Next
From: Rob Sargent
Date:
Subject: Re: update from join