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 1h9fjf-0001WD-BJ for pgsql-general@arkaria.postgresql.org; Fri, 29 Mar 2019 00:51: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 1h9fjd-0005P6-Dq for pgsql-general@arkaria.postgresql.org; Fri, 29 Mar 2019 00:51:05 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h9fjd-0005Oz-01 for pgsql-general@lists.postgresql.org; Fri, 29 Mar 2019 00:51:05 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1h9fjV-0008DY-Rq for pgsql-general@lists.postgresql.org; Fri, 29 Mar 2019 00:51:03 +0000 Received: from [192.168.127.1] (port=49948 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1h9fjS-000Orh-AZ; Fri, 29 Mar 2019 00:50:54 +0000 Received: from [127.0.0.1] (port=62426 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1h9fjR-000AIb-Ii; Fri, 29 Mar 2019 00:50:53 +0000 From: Andrew Gierth To: Rich Shepard Cc: "pgsql-generallists.postgresql.org" Subject: Re: Subquery to select max(date) value In-Reply-To: (Rich Shepard's message of "Thu, 28 Mar 2019 16:21:45 -0700 (PDT)") Message-ID: <87mulev72k.fsf@news-spur.riddles.org.uk> 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: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Fri, 29 Mar 2019 00:50:53 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Rich" == Rich Shepard writes: Rich> Tried this and did not do it correctly. Should there be two Rich> 'order by', one in the sub-query, the other in the top-level Rich> query? Yes. Rich> This does not return the desired order: Rich> select p.person_id, p.lname, p.fname, p.direct_phone, p.active, o.org_name, sq.* Rich> from people as p Rich> join organizations as o on p.org_id = o.org_id Rich> cross join Rich> lateral Rich> (select a.next_contact Rich> from activities as a Rich> where a.person_id = p.person_id and Rich> p.active='True' and Rich> a.next_contact is not null Rich> order by a.next_contact DESC Rich> limit 1) sq Rich> order by sq.next_contact DESC; That query seems correct assuming you want the result in descending order of next_contact. How did the actual result differ from your expectation? -- Andrew (irc:RhodiumToad)