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 1gulaa-0005LD-IW for pgsql-general@arkaria.postgresql.org; Fri, 15 Feb 2019 22:04:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gulaX-0006Lw-Pw for pgsql-general@arkaria.postgresql.org; Fri, 15 Feb 2019 22:04: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 1gulaX-0006Lp-EC for pgsql-general@lists.postgresql.org; Fri, 15 Feb 2019 22:04:05 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gulaU-0003km-2x for pgsql-general@lists.postgresql.org; Fri, 15 Feb 2019 22:04:04 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id DE18299356 for ; Fri, 15 Feb 2019 14:04:00 -0800 (PST) Date: Fri, 15 Feb 2019 14:04:00 -0800 (PST) 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: > Rich> I've not before run 'explain' on a query. Would that be > Rich> appropriate here? > > Yes. Andrew, I'll learn how to use it. > The problem here is that you have no join conditions at all, so the > result set of this query is massive. And you've duplicated many tables > inside the subquery which is not necessary or appropriate. Got it now. > 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; > > Ordering by DESC with a limit 1 is used to get the max next_contact > value rather than the smallest; this is similar to max(), but makes it > trivial to also access the other columns of the _same_ activities row > which is being selected. This puts everything in perspective and is a very valuable lesson for me as this application has many queries of this type. You've put together all that I've read in the manual, on this mail list thread, and on web pages. I really appreciate your patient guidance. Best regards, Rich