Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d6Ywm-0000OX-Cg for pgsql-sql@arkaria.postgresql.org; Fri, 05 May 2017 08:50:44 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1d6Ywl-0007zl-Qx for pgsql-sql@arkaria.postgresql.org; Fri, 05 May 2017 08:50:43 +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.84_2) (envelope-from ) id 1d6Ywl-0007yz-4w for pgsql-sql@postgresql.org; Fri, 05 May 2017 08:50:43 +0000 Received: from lb2-smtp-cloud6.xs4all.net ([194.109.24.28]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1d6Ywi-0007mc-2c for pgsql-sql@postgresql.org; Fri, 05 May 2017 08:50:42 +0000 Received: from webmail.xs4all.nl ([IPv6:2001:888:0:22:194:109:20:208]) by smtp-cloud6.xs4all.net with ESMTP id GYqc1v00C4ydcfa01Yqent; Fri, 05 May 2017 10:50:38 +0200 Received: from mail.genj.nl ([77.242.116.162]) by webmail.xs4all.nl with HTTP (HTTP/1.1 POST); Fri, 05 May 2017 10:50:36 +0200 MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit Date: Fri, 05 May 2017 10:50:36 +0200 From: vinny To: Gary Stainburn Cc: pgsql-sql@postgresql.org, pgsql-sql-owner@postgresql.org Subject: Re: Most recent row In-Reply-To: <201705050925.04194.gary.stainburn@ringways.co.uk> References: <201705050925.04194.gary.stainburn@ringways.co.uk> Message-ID: <7690a8d6f9fd1ce96b89f97425d8ae33@xs4all.nl> X-Sender: vinny@xs4all.nl User-Agent: XS4ALL Webmail X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 2017-05-05 10:25, Gary Stainburn wrote: > This question has been asked a few times, and Google returns a few > different > answers, but I am interested people's opinions and suggestions for the > *best* > wat to retrieve the most recent row from a table. > > My case is: > > create table people ( > p_id serial primary key, > ...... > ); > > create table assessments ( > p_id int4 not null references people(p_id), > as_timestamp timestamp not null, > ...... > ); > > select p.*, (most recent) a.* > from people p, assessments a > .. > ; It depends on what you mean by 'most recent'. Do you mean "the row I just inserted" or "the row with a date closest to now()" or perhaps some other definition? -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql