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 1gjRfi-0001ch-EG for pgsql-general@arkaria.postgresql.org; Tue, 15 Jan 2019 16:34:38 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gjRfg-0002BL-HD for pgsql-general@arkaria.postgresql.org; Tue, 15 Jan 2019 16:34:36 +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 1gjRfg-0002BE-6A for pgsql-general@lists.postgresql.org; Tue, 15 Jan 2019 16:34:36 +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 1gjRfd-0007mT-KP for pgsql-general@lists.postgresql.org; Tue, 15 Jan 2019 16:34:35 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id 5566C9934E for ; Tue, 15 Jan 2019 08:34:32 -0800 (PST) Date: Tue, 15 Jan 2019 08:34:32 -0800 (PST) From: Rich Shepard To: pgsql-general@lists.postgresql.org Subject: Re: Refining query statement In-Reply-To: <9a5976c8-76da-f06d-9840-5dcf8723e38b@gmx.net> Message-ID: References: <9a5976c8-76da-f06d-9840-5dcf8723e38b@gmx.net> 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 Tue, 15 Jan 2019, Thomas Kellerer wrote: > With regards to "cleaner": the first thing to do is to remove the > parentheses around the column list. In Postgres "(a,b,c)" creates a single > column with an anonymous record type (that contains three fields), rather > than selecting three columns. In other DBMS those parentheses are simply > useless. Thomas, I should have seen that myself. Thanks for pointing out the obvious to me. > "cleaner" is always subjective, but I find explicit JOIN operators a lot > cleaner than the old implicit joins. This does make sense; I need to refresh my knowledge of JOIN operators and will do so. > The condition "A.next_contact is not null" is actually no necessary > because you already have a condition on that column, so NULL values won't > be returned anyway. True that. > To get the "most recent one" in Postgres, DISTINCT ON () is usually the > best way to do it: So we end up with something like this: > > select distinct on (C.contact_id) C.contact_id, C.lname, C.fname, C.direct_phone, O.org_name, A.next_contact > from Contacts as C > join Organizations as O on C.org_id = O.org_id > join Activities as A on C.contact_id = A.contact_id > where A.next_contact <= 'today' > and A.next_contact > '2018-12-31' > order by c.contact_id, a.next_contact DESC; This insight really helps. Now I know how to approach other queries. Many thanks, Rich