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 1gjQwE-0007TR-7S for pgsql-general@arkaria.postgresql.org; Tue, 15 Jan 2019 15:47: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 1gjQwC-0002c7-WE for pgsql-general@arkaria.postgresql.org; Tue, 15 Jan 2019 15:47:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gjQwC-0002c0-Ou for pgsql-general@lists.postgresql.org; Tue, 15 Jan 2019 15:47:36 +0000 Received: from mout.kundenserver.de ([217.72.192.75]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gjQwA-0007hl-OJ for pgsql-general@lists.postgresql.org; Tue, 15 Jan 2019 15:47:36 +0000 Received: from [10.8.0.101] ([185.40.248.10]) by mrelayeu.kundenserver.de (mreue109 [212.227.15.145]) with ESMTPSA (Nemesis) id 1Mgek6-1hB6BE2xW0-00h91O for ; Tue, 15 Jan 2019 16:47:32 +0100 Subject: Re: Refining query statement To: pgsql-general@lists.postgresql.org References: From: Thomas Kellerer Message-ID: <9a5976c8-76da-f06d-9840-5dcf8723e38b@gmx.net> Date: Tue, 15 Jan 2019 16:47:32 +0100 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-GB Content-Transfer-Encoding: 8bit X-Provags-ID: V03:K1:LgiJNJnkEICX7A4n/ltm6aHdYJwHSe2iyXNt94k7Hm8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mOm8MX3H4aZd0pOngEQfsQ5105r/txTNGbldxoEzAM/OjhJ1GZyfS List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Rich Shepard schrieb am 15.01.2019 um 16:39: >   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. 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. "cleaner" is always subjective, but I find explicit JOIN operators a lot cleaner than the old implicit joins. 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. 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;