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 1gjQpA-00074j-2Q for pgsql-general@arkaria.postgresql.org; Tue, 15 Jan 2019 15:40:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gjQo9-0008MW-JZ for pgsql-general@arkaria.postgresql.org; Tue, 15 Jan 2019 15:39:17 +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 1gjQo9-0008MO-8J for pgsql-general@lists.postgresql.org; Tue, 15 Jan 2019 15:39:17 +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 1gjQo2-0006Xn-7X for pgsql-general@lists.postgresql.org; Tue, 15 Jan 2019 15:39:15 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id 6BDD99934E for ; Tue, 15 Jan 2019 07:39:08 -0800 (PST) Date: Tue, 15 Jan 2019 07:39:08 -0800 (PST) From: Rich Shepard Reply-To: Rich Shepard To: pgsql-general@lists.postgresql.org Subject: Refining query statement Message-ID: 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 Working with my sales/client management system using psql I have a select statement to identify contacts to be made. This statement works: select (C.contact_id, C.lname, C.fname, C.direct_phone, O.org_name, A.next_contact) from Contacts as C, Organizations as O, Activities as A where C.org_id = O.org_id and C.contact_id = A.contact_id and A.next_contact <= 'today' and A.next_contact > '2018-12-31' and A.next_contact is not null; but would benefit from tweaking. When I have had multiple contacts with someone I want only the most recent one displayed, not all, and they should be no more ancient than a defined period (e.g., a month). I want to learn how to make this query cleaner and more flexible. When I write the UI for this I want to be able to specify a data range in addition to a fixed 'today'. Pointers on what to read will be very helpful. TIA, Rich