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 1gAAYY-0004MJ-3i for pgsql-sql@arkaria.postgresql.org; Wed, 10 Oct 2018 09:13:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gAAYV-0001aS-2E for pgsql-sql@arkaria.postgresql.org; Wed, 10 Oct 2018 09:13:23 +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 1gAAYU-0001aL-MX for pgsql-sql@lists.postgresql.org; Wed, 10 Oct 2018 09:13:22 +0000 Received: from mail.inqbus.de ([164.177.165.236]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gAAYS-0007Bl-Fl for pgsql-sql@lists.postgresql.org; Wed, 10 Oct 2018 09:13:22 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=stb-datenservice.de; s=20160215; h=Content-Type:In-Reply-To:MIME-Version: Date:Message-ID:From:References:To:Subject:Sender:Reply-To:Cc: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=fPmg6hd8Bov7c0canIfFObdYonbYtOtMTJ8bVJdBt7c=; b=yOvktiK7lS+58qwsYqqXJ5SE/ 4F3LV+98hGarYgHluTKxAJa+Yq1igNWwBhw77RCk02D4aSkc3CeT9gWRBjfvJ+QWnR/AYZzKJCjhU XFTRzPZGokZgin3VQpeewUKWeW6kBBWmwUQLaCNOCO5yKy75Vfqc3UHtyGaXH33wweuVg=; Received: from p578c3f48.dip0.t-ipconnect.de ([87.140.63.72]:63080 helo=[192.168.178.254]) by mail.inqbus.de with esmtpa (Exim 4.89) (envelope-from ) id 1gAAYQ-0003YN-ME for pgsql-sql@lists.postgresql.org; Wed, 10 Oct 2018 11:13:18 +0200 Subject: Re: left outer join to pull in most recent record To: pgsql-sql@lists.postgresql.org References: <201810091418.01620.gary.stainburn@ringways.co.uk> From: "MS (direkt)" Message-ID: <5abe34aa-b5a3-7ad4-7e62-960ab86e201e@stb-datenservice.de> Date: Wed, 10 Oct 2018 11:13:15 +0200 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.2.1 MIME-Version: 1.0 In-Reply-To: <201810091418.01620.gary.stainburn@ringways.co.uk> Content-Type: multipart/alternative; boundary="------------70B5059171F9166CC3CD7BCD" Content-Language: de List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------70B5059171F9166CC3CD7BCD Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit select distinct s_regno, max(s_stock_no) over (partition by s_regno order by s_regno) from stock; Am 09.10.2018 um 15:18 schrieb Gary Stainburn: > 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 ........... > -- Widdersdorfer Str. 415, 50933 Köln; Tel. +49 / 221 / 9544 010 HRB Köln HRB 75439, Geschäftsführer: S. Böhland, S. Rosenbauer --------------70B5059171F9166CC3CD7BCD Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit select distinct s_regno, max(s_stock_no) over (partition by s_regno order by s_regno) from stock;

Am 09.10.2018 um 15:18 schrieb Gary Stainburn:
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 ...........


-- 

Widdersdorfer Str. 415, 50933 Köln; Tel. +49 / 221 / 9544 010
HRB Köln HRB 75439, Geschäftsführer: S. Böhland, S. Rosenbauer 
--------------70B5059171F9166CC3CD7BCD--