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 1gkAWQ-0003cE-NP for pgsql-general@arkaria.postgresql.org; Thu, 17 Jan 2019 16:28:03 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gkAWO-0008Nh-Qf for pgsql-general@arkaria.postgresql.org; Thu, 17 Jan 2019 16:28:00 +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 1gkAWM-0008J2-Tu for pgsql-general@lists.postgresql.org; Thu, 17 Jan 2019 16:28:00 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gkAWJ-0002YS-PW for pgsql-general@lists.postgresql.org; Thu, 17 Jan 2019 16:27:57 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id ED0F5217D0; Thu, 17 Jan 2019 11:27:54 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 17 Jan 2019 11:27:54 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=m 78KZ0nfruqTX0LrRxuw9a+pI3B48JUArT6VR4n7MlY=; b=MLP1UwcaH8CCKQVZo az0/tapmsrX2rXmPuRX8wv8FPCggdgUCgNemT3crJqwnRNL7ovXsb7uP35Ju8plC rNuCXfOhZEOkO/LNyMwt6CGucsjM0XteUZfFpwDbu6Xow5sk3jxH/GZwaQwOI7sa MuA712HboBnOztsJ9c1zU0WaeUTFMSR1ZRdAZtnjh8MB1xrZ7qYgSYWUK1nZYnBN pOpirYHKz6eDpsL8Uxsh2aelWJ4X6ufMmRQuxWeCzyb+OPYbZYFefBVmXYIhNzzn 7OPQsayqIgNp7wZGKkrzN1Ym0J1Hgh0e8tZGAdCmpgkodeF6VUf3Edpik87q4EhQ jxG1Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=m78KZ0nfruqTX0LrRxuw9a+pI3B48JUArT6VR4n7M lY=; b=bBSBLccV+8I43K7itWAvq/nZRqbWRlR8zcHwF0RnsJ6dLRKmsqMez7esJ Vu2logW7HEqh5DkcgsPpc3d9gY5ikRi+0fuCwPGvo9x2i/xTSYzEjrCPQKAYM3Gh W65UoiZTTt7KPgml0OjufF9TiIGZ7h7r9HeQmgMyAO9iPep3dfn9qiC8EVKMFo0p d7stelvoiXhX/iPKHukgEKoEvw/aM8Wzlpqp1P5+x6BmWG0uLwRf8TIRBFjwQTni eBPDoBPSRY2ca22X/oXBWnmrtfcmsPYPoDGmdML7vwNl4974s1mBgLWz130ShCOL 13dOaiv4HiDuJYKsbonX72cSOug+g== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrgeekgdekvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthenuceurghilhhouhhtmecufedt tdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhepuffvfhfhkffffg ggjggtgfesthekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegr ughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucfkphepjedurddvud dvrddufeegrdefnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghv vghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.2] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id AA82CE425A; Thu, 17 Jan 2019 11:27:53 -0500 (EST) Subject: Re: Refining query statement To: Rich Shepard , pgsql-general@lists.postgresql.org References: <9a5976c8-76da-f06d-9840-5dcf8723e38b@gmx.net> From: Adrian Klaver Message-ID: <2070c41e-8944-d1a5-a8a9-b42f717e9ac7@aklaver.com> Date: Thu, 17 Jan 2019 08:27:51 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 1/17/19 8:14 AM, Rich Shepard wrote: > 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. ? ... WHERE next_contact BETWEEN '01/01/2019'::date AND 'today'::date AND a.next_contact IS NOT NULL ORDER BY c.contact_id, a.next_contact DESC; > > Looking forward to learning, > > Rich > > -- Adrian Klaver adrian.klaver@aklaver.com