Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g9rtn-0005w5-Eu for pgsql-sql@arkaria.postgresql.org; Tue, 09 Oct 2018 13:18:07 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1g9rtl-00007R-No for pgsql-sql@arkaria.postgresql.org; Tue, 09 Oct 2018 13:18:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g9rtl-00007J-H9 for pgsql-sql@lists.postgresql.org; Tue, 09 Oct 2018 13:18:05 +0000 Received: from hub.ringways.co.uk ([88.211.105.30] helo=ringways.co.uk) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1g9rtj-0007RY-Fo for pgsql-sql@lists.postgresql.org; Tue, 09 Oct 2018 13:18:05 +0000 Received: from eddie.ringways.co.uk ([10.1.1.115]) by ringways.co.uk with esmtp (Exim 4.90_1) (envelope-from ) id 1g9rth-0009JR-VA for pgsql-sql@lists.postgresql.org; Tue, 09 Oct 2018 14:18:02 +0100 From: Gary Stainburn Organization: Ringways Garages Ltd To: "pgsql-sql@lists.postgresql.org" Subject: left outer join to pull in most recent record Date: Tue, 9 Oct 2018 14:18:01 +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: <201810091418.01620.gary.stainburn@ringways.co.uk> X-Spam-Score: -50.7 (--------------------------------------------------) X-Spam-Report: Spam detection software, running on the system "ollie2.ringways.co.uk", has NOT identified this incoming email as spam. The original message has been attached to this so you can view it or label similar future email. If you have any questions, see Gary Stainburn for details. Content preview: I have the following stock list table. # select * from stock; s_stock_no | s_regno +--------- N12345 | GPS1 N12346 | TEST1 U123 | GPS1 (3 rows) This shows two vehicles. GPS1 was originally a new vehicle which we then sold. TEST1 is also a new vehicle GPS1 is sunsequently purchased back from the customer as a part exchange, and is put on the stock table as a used car. [...] Content analysis details: (-50.7 points, 15.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -50 ALL_TRUSTED Passed through trusted hosts only via SMTP -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] 0.1 SCORE_RCPTS Adding score for each recipient 0.1 SCORE_RCPTS Adding score for each recipient 0.0 AWL AWL: Adjusted score from AWL reputation of From: address 1.0 RING_SAFE No description available. List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk I have the following stock list table. # select * from stock; s_stock_no | s_regno ------------+--------- N12345 | GPS1 N12346 | TEST1 U123 | GPS1 (3 rows) This shows two vehicles. GPS1 was originally a new vehicle which we then sold. TEST1 is also a new vehicle GPS1 is sunsequently purchased back from the customer as a part exchange, and is put on the stock table as a used car. If I later want to do a search based on the registration number, wishing to retrieve the most recent record what is the *best* (quickest or least CPU time depending on your preference) way to do this? By most recent record, I mean the record with the highest stock number. I'm looking for a solution to do a straight select where..... but I am also looking for a solution that can be used in a left join, for example select diary.*, stock.* from diary left outer join stock on ...........