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 1gkAJC-0002zN-Vj for pgsql-general@arkaria.postgresql.org; Thu, 17 Jan 2019 16:14:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gkAJA-0007Ps-FK for pgsql-general@arkaria.postgresql.org; Thu, 17 Jan 2019 16:14:20 +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 1gkAJA-0007Ng-3G for pgsql-general@lists.postgresql.org; Thu, 17 Jan 2019 16:14:20 +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 1gkAJ7-0002CJ-J7 for pgsql-general@lists.postgresql.org; Thu, 17 Jan 2019 16:14:18 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id 61C0B99353 for ; Thu, 17 Jan 2019 08:14:16 -0800 (PST) Date: Thu, 17 Jan 2019 08:14:16 -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: > 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; Combining this with Adrian's advice to use BETWEEN I have this statement that almost works: 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 next_contact BETWEEN '01/01/2019'::date AND 'today'::date ORDER BY c.contact_id, a.next_contact DESC; It fails when the most recent next_contact column in Activities is NULL and an earier row has a non-NULL date in the specified range. I tried specifying max(a.next_contact) and added GROUP BY, but the result set all returned o.org_name columns to the same one. The WHERE clause needs to exclude a contact_id where the most current row in Activities has NULL for the next_contact column. I've tried a few ideas but none work so I need to learn the proper syntax, and I don't find that in Rick van der Lans' or Joe Celko's books I have. Looking forward to learning, Rich