Received: from magus.postgresql.org (magus.postgresql.org [87.238.57.229]) by mail.postgresql.org (Postfix) with ESMTP id BD00C1B52349 for ; Wed, 23 May 2012 06:47:46 -0300 (ADT) Received: from hub.ringways.co.uk ([77.86.27.30] helo=mail.ringways.co.uk) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SX8AS-0007Um-6Z for pgsql-sql@postgresql.org; Wed, 23 May 2012 09:47:45 +0000 Received: from localhost ([127.0.0.1] helo=mail.ringways.co.uk) by mail.ringways.co.uk with esmtp (Exim 4.69) (envelope-from ) id 1SX8AB-0004zA-HY for pgsql-sql@postgresql.org; Wed, 23 May 2012 10:47:29 +0100 Received: from eddie.ringways.co.uk ([10.1.1.115] helo=eddie.ringways.co.uk) by mail.ringways.co.uk with ESMTP id qAlOm84o34949; Wed, 23 May 2012 10:47:24 +0100 From: Gary Stainburn Organization: Ringways Garages Ltd To: pgsql-sql@postgresql.org Subject: Re: left outer join only select newest record Date: Wed, 23 May 2012 10:47:27 +0100 User-Agent: KMail/1.9.10 References: <201205231027.39198.gary.stainburn@ringways.co.uk> <53C6FD0FE48B43C0AD419648FDC8D943@marktestcr.marktest.pt> In-Reply-To: <53C6FD0FE48B43C0AD419648FDC8D943@marktestcr.marktest.pt> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201205231047.27372.gary.stainburn@ringways.co.uk> X-SpamTest-Envelope-From: gary.stainburn@ringways.co.uk X-SpamTest-Info: Profiles 20401 [Mar 31 2011] X-SpamTest-Method: none X-SpamTest-Rate: 0 X-SpamTest-Status: Not detected X-SpamTest-Status-Extended: not_detected X-SpamTest-Version: SMTP-Filter Version 3.0.0 [0285], KAS30/SDK/Release X-Anti-Virus: Kaspersky Mail Gateway, version: 5.6.28/RELEASE, bases: 20110331T110535 #5151353, check: 20120523 clean X-Spam-Score: -51.6 (---------------------------------------------------) X-Spam-Report: Spam detection software, running on the system "ollie.ringways.co.uk", has identified this incoming email as possible spam. The original message has been attached to this so you can view it (if it isn't spam) or label similar future email. If you have any questions, see Gary Stainburn for details. Content preview: Appologies for not making it clearer. stock_details is simply a view of table stock, pulling in some lookup values. used_diary is the name of the table containing the tax requests. It's called the used_diary because it was the diary for taxing used vehicles. [...] Content analysis details: (-51.6 points, 15.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -50 ALL_TRUSTED Passed through trusted hosts only via SMTP -2.6 BAYES_00 BODY: Bayesian spam probability is 0 to 1% [score: 0.0000] 1.0 RING_SAFE RING_SAFE X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201205/66 X-Sequence-Number: 36614 Appologies for not making it clearer. stock_details is simply a view of table stock, pulling in some lookup values. used_diary is the name of the table containing the tax requests. It's called the used_diary because it was the diary for taxing used vehicles. Here is a select to show the problem. There is one stock record and two tax records. What I'm looking for is how I can return only the second tax record, the one with the highest ud_id select s_stock_no, s_regno, s_vin, s_created, ud_id, ud_handover_date from stock s left outer join used_diary u on s.s_regno = u.ud_pex_registration where s_stock_no = 'UL15470'; s_stock_no | s_regno | s_vin | s_created | ud_id | ud_handover_date ------------+---------+-------------------+----------------------------+-------+------------------ UL15470 | YG12*** | KNADN312LC6****** | 2012-05-21 09:15:31.569471 | 41892 | 2012-04-06 UL15470 | YG12*** | KNADN312LC6****** | 2012-05-21 09:15:31.569471 | 42363 | 2012-05-16 (2 rows) On Wednesday 23 May 2012 10:37:31 Oliveiros d'Azevedo Cristina wrote: > Gary, > > You describe two tables vehicle stock and tax requests. The former has a > one-to-many relationship wit the second one, right? > > But your query involves stock details and used_diary. > > What is the relationship of these two new tables to the previous ones? > > Could you please kindly supply an example of what you have and of the > desired output? For me it would be easier... > > Best, > Oliver > > ----- Original Message ----- > From: "Gary Stainburn" > To: > Sent: Wednesday, May 23, 2012 10:27 AM > Subject: [SQL] left outer join only select newest record > > > Hi folks, > > > > I know I've seen posts like this before but Google isn't helping today. > > > > I have two tables, vehicle stock and tax requests. Each vehicle can be > > taxed > > more than once, but I only want to pull in the most recent tax request - > > the > > one with the highest ud_id. > > > > I have the following, which obviously returning multiple records which > > then > > appears that the same vehicle is in stock multiple times. How can I make > > it > > so we only show each vehicle once, showing the most recent tax request > > details. > > > > > > select * from stock_details s > > left outer join used_diary u on s.s_registration = u.ud_registration; > > > > > > -- > > Gary Stainburn > > Group I.T. Manager > > Ringways Garages > > http://www.ringways.co.uk > > > > -- > > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > > To make changes to your subscription: > > http://www.postgresql.org/mailpref/pgsql-sql -- Gary Stainburn Group I.T. Manager Ringways Garages http://www.ringways.co.uk