Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d6fgk-0008BN-0S for pgsql-sql@arkaria.postgresql.org; Fri, 05 May 2017 16:02:38 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1d6fgj-00069N-1o for pgsql-sql@arkaria.postgresql.org; Fri, 05 May 2017 16:02:37 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1d6fgg-00061B-8j for pgsql-sql@postgresql.org; Fri, 05 May 2017 16:02:34 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1d6fgY-0005I8-Nk for pgsql-sql@postgresql.org; Fri, 05 May 2017 16:02:33 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 55FF4208A4; Fri, 5 May 2017 12:02:25 -0400 (EDT) Received: from frontend2 ([10.202.2.161]) by compute6.internal (MEProxy); Fri, 05 May 2017 12:02:25 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc:x-sasl-enc; s=fm1; bh=XPGnnEJ0pIGVoGJ99c 1sdauryy4lqdEYfKBJWESzCJQ=; b=MVBv424K9SaVFsX8ka3jXtxornbn6bJFlP wfZEsCTvqPcDW2GNkRiHjYNG8+cOi3dO5UqIOfvUf+OxgIdiXvUckGTONNNHrcwp ro/1U9aGgJbBRF/9ktFRwC0R10f+kIV7CJCf4c8AHkg1lpqOaAbTzL3uEjma1K1n gBlZWwlxVDyPJTPtGOSUt11ylMhtyJwmyrbCEm4SZaFgkG+vDsGxzhZzSruBK3lz dNkvwpSRZWGRFwqfA7RWHw1HlXkwuqQ6KPJMMUUgBNK5hmDY4AQYTd1GOgR/sDfe eNNmfDjCwGFOrFnT8srpjiR5HNehVmX0CzzqMxqrgyCBcTaaX8dg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc:x-sasl-enc; s= fm1; bh=XPGnnEJ0pIGVoGJ99c1sdauryy4lqdEYfKBJWESzCJQ=; b=CALdrhdJ LShFP/hjbuhVdTwcKBsgdy4NxhCXu1Emzvj1xflWZLI99lyaNXnBgHJDVSRX3k6e phrPE0ZpeT0KyoNvieualJVjpqAWggQWu5KuiSeFlUfNyOCY252iYrz6BkCxsRat cF0g+5iz05mMdFhK373gn/JONpdYZhr1bDCZfn/7R083JW+v10NAEhd0PvaBjtO2 hgxnvZXtEsyLEWBb/ofhuX+zVCDNhG4XPXs780XdeEUGvF/f9I3l2GVA82XYZxgc gjP72zPTdkqa2RX9FjrVCuE9Ld6Qs0cK++UCFjvUnzmUt8ry69s7Qd5HtwnHUgMr owKdeblMGXsOng== X-ME-Sender: X-Sasl-enc: ewi+P8AZMoprGvet2qRfmdg5oN7W2CylYo80DMAiz4aU 1494000145 Received: from ranger.suse (c-73-225-242-52.hsd1.wa.comcast.net [73.225.242.52]) by mail.messagingengine.com (Postfix) with ESMTPA id BD9FE240A5; Fri, 5 May 2017 12:02:24 -0400 (EDT) Subject: Re: Most recent row To: "David G. Johnston" , Gary Stainburn References: <201705050925.04194.gary.stainburn@ringways.co.uk> Cc: "pgsql-sql@postgresql.org" From: Adrian Klaver Message-ID: Date: Fri, 5 May 2017 09:02:24 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Pg-Spam-Score: -2.7 (--) 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 05/05/2017 08:14 AM, David G. Johnston wrote: > On Fri, May 5, 2017 at 1:25 AM, 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 > .. > ; > > > ​I would start with something using DISTINCT ON and avoid redundant > data. If performance starts to suck I would then probably add a field > to people where you can record the most recent assessment id and which > you would change via a trigger on assessments. > > (not tested)​ > > ​SELECT DISTINCT ON (p) p, a > FROM people p > LEFT JOIN ​assessments a USING (p_id) > ORDER BY p, a.as_timestamp DESC; > > David J. > My take: create table people(p_id SERIAL PRIMARY KEY, name_first VARCHAR, name_last VARCHAR); create table assessments(p_id INTEGER NOT NULL REFERENCES people(p_id), as_timestamp TIMESTAMP NOT NULL) insert into people(name_first, name_last) values ('Daffy', 'Duck'), ('Mickey', 'Mouse'), ('Rocky', 'Squirrel'); insert into assessments (p_id, as_timestamp) values (1, '09/12/2016'), (3, '10/01/2016'), (2, '11/14/2016'), (1, '12/27/2016'), (2,'01/03/2017'),(3, '02/23/2017'), (1, '03/05/2017'); SELECT * FROM people JOIN ( SELECT p_id, max(as_timestamp) AS last_assessment FROM assessments GROUP BY p_id) AS max_ts ON people.p_id = max_ts.p_id JOIN assessments AS a ON a.p_id = max_ts.p_id AND a.as_timestamp = max_ts.last_assessment ORDER BY people.p_id; p_id | name_first | name_last | p_id | max ------+------------+-----------+------+--------------------- 1 | Daffy | Duck | 1 | 2017-03-05 00:00:00 2 | Mickey | Mouse | 2 | 2017-01-03 00:00:00 3 | Rocky | Squirrel | 3 | 2017-02-23 00:00:00 (3 rows) -- Adrian Klaver adrian.klaver@aklaver.com -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql