Re: update from join
От | Rob Sargent |
---|---|
Тема | Re: update from join |
Дата | |
Msg-id | [email protected] обсуждение исходный текст |
Ответ на | update from join (Gary Stainburn <[email protected]>) |
Список | pgsql-sql |
I wonder if this works:<br /><br />update stock s set s_superceded = true <br />where s.s_updated < (select max(t.s_updated)from stock t where t.s_vin = s.s_vin)<br /><br /><br /><br /><div class="gmail_quote">On Thu, May 14, 2009at 7:27 AM, Gary Stainburn <span dir="ltr"><<a href="mailto:[email protected]">[email protected]</a>></span>wrote:<br /><blockquote class="gmail_quote"style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">I knowI should be able to do this but my brain's mashed today<br /><br /> I have a stock table with<br /><br /> s_stock_no varchar primary key<br /> s_vin varchar<br /> s_updated timestamp<br/> s_superceded boolean<br /><br /> It is possible for the same vin to exist on stock if we havesold and then<br /> bought back a vehicle, e.g. as a part exchange.<br /><br /> Every time a vehicle is inserted/updatedthe s_updated field is update.<br /><br /> How can I update the table so that for each s_vin, if a recorddoes not have<br /> the most recent s_updated value, s_superceded is set to true?<br /><br /> I can get the most recentvalue by running:<br /><br /> select * from (select s_vin,<br /> count(s_updated) as numb,<br /> max(s_updated)::timestampas latest<br /> from stock<br /> group by s_vin) foo<br /> where numb > 1;<br /><br /><br/> but I can't seem to get how I can convert this to an update statement. The num<br /> > 1 simply removed all vehicleswith only one record.<br /><br /> I seem to think I need an update..... from..... statement<br /><br /> --<br />Gary Stainburn<br /><br /> This email does not contain private or confidential material as it<br /> may be snooped on byinterested government parties for unknown<br /> and undisclosed purposes - Regulation of Investigatory Powers Act, 2000<br/><font color="#888888"><br /> --<br /> Sent via pgsql-sql mailing list (<a href="mailto:[email protected]">[email protected]</a>)<br/> To make changes to your subscription:<br /><a href="http://www.postgresql.org/mailpref/pgsql-sql"target="_blank">http://www.postgresql.org/mailpref/pgsql-sql</a><br /></font></blockquote></div><br/>
В списке pgsql-sql по дате отправления: