Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h9dzS-0001gU-8t for pgsql-general@arkaria.postgresql.org; Thu, 28 Mar 2019 22:59:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1h9dzQ-0003op-Ts for pgsql-general@arkaria.postgresql.org; Thu, 28 Mar 2019 22:59:16 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1h9dzQ-0003nm-JE for pgsql-general@lists.postgresql.org; Thu, 28 Mar 2019 22:59:16 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h9dzO-0004Cm-CG for pgsql-general@lists.postgresql.org; Thu, 28 Mar 2019 22:59:16 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id 9314D9936E for ; Thu, 28 Mar 2019 15:59:11 -0700 (PDT) Date: Thu, 28 Mar 2019 15:59:11 -0700 (PDT) From: Rich Shepard To: pgsql-general@lists.postgresql.org Subject: Re: Subquery to select max(date) value In-Reply-To: <874l94lo43.fsf@news-spur.riddles.org.uk> Message-ID: References: <2780074d-3b87-b154-8bc6-a0f898fbdde2@openvistas.net> <8b99480a-7711-694e-a1c5-ae65bb0c0364@aklaver.com> <87va1oplgl.fsf@news-spur.riddles.org.uk> <878syglqvp.fsf@news-spur.riddles.org.uk> <874l94lo43.fsf@news-spur.riddles.org.uk> User-Agent: Alpine 2.20 (LNX 67 2015-01-07) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On Fri, 15 Feb 2019, Andrew Gierth wrote: > select p.person_id, p.lname, p.fname, p.direct_phone, o.org_name, sq.* > from people as p > join organizations as o on p.organization_id=o.id -- OR WHATEVER > cross join > lateral (select a.next_contact > from activities as a > where a.person_id=p.person_id --VERY IMPORTANT > and a.next_contact > '2018-12-31' > and a.next_contact <= 'today' > and a.next_contact is not null > order by a.next_contact DESC > limit 1) sq; After working with this query I modified it slightly to return only the next_contact date: select p.person_id, p.lname, p.fname, p.direct_phone, p.active, o.org_name, sq.* from people as p join organizations as o on p.org_id = o.org_id cross join lateral (select a.next_contact from activities as a where a.person_id = p.person_id and p.active='True' and a.next_contact is not null order by a.next_contact DESC limit 1) sq; It works wellm, but the row order is not that of a.next_contact. In fact, there seems to be no order in the returned set. The next_contact column is in the lateral sub-query. Does this make a difference? I've no idea how to modify the query so that returned rows are in decreasing next_contact order. Best regards, Rich