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 1gkAjZ-0004H2-Em for pgsql-general@arkaria.postgresql.org; Thu, 17 Jan 2019 16:41:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gkAjY-0007jB-1G for pgsql-general@arkaria.postgresql.org; Thu, 17 Jan 2019 16:41: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 1gkAjX-0007j3-MP for pgsql-general@lists.postgresql.org; Thu, 17 Jan 2019 16:41:35 +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 1gkAjR-0002t5-3v for pgsql-general@lists.postgresql.org; Thu, 17 Jan 2019 16:41:34 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 5BA6C20B4B; Thu, 17 Jan 2019 11:41:28 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 17 Jan 2019 11:41:28 -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=I 6liyCfSG1pl4awHJhGIFJdaoIyI2aVqIRjXnerge7o=; b=JQ1QZeLkyWSRGIsKK sJP/SBwdijqLA+9N38CLF5fldJc/kwpYilncoWx4NEmW9XNctnVmEf8zFXUYRq5m zHtAb7PthsiIyi5XS+4sghKmxCClG6BeHaDupRNxdp1DAM1APpCxfNWOpS0YBtA8 gWuzMIksJ7/j5wWGhbI+IX/jY5e2r2l6nVX34FUshoVxMjlexua/TXgxTMNja6wC JlOGtAMtgw0wuBjV74tpWcDApgHY/ElzgFMC4KMzW4/Z7E6mQxU7FcYlDoXQk722 lCfyAnCy7LvClOcq9sar6+A6rX62zOsoyzPxuFxVU4oPywIdxUpz4oAGc39TvRNm yawlg== 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=I6liyCfSG1pl4awHJhGIFJdaoIyI2aVqIRjXnerge 7o=; b=W355qI92s66R7JgYQmnms1xITcqO6asX/THC4uINtwxBJKgdoP1WUKsLN Phpjfhi3ojiLei9BJ9sTX5IyYkGZYPsada0ftGuG7mGnjKWoYe3Qns7I3h/8mOFF dlyNsz2l/RBtDSFKlOsHQFr9IauvGvN4nw3EMNy2GeVCzESZnuKK5uoLHnfuj5SB DZQn+Bap11ZWk5UPwcbxnRX2a0ZNtDZjt6UgjhE19v9S8RoTpm6nDHa4aSa9c1Lp JL1uL6A4tTUg3lb1Fv17uPa0DkFo05YUYzz9gYp+w/d4q+muUDFGfSlz2F+FBmtm bf3WT9EpX0/M/x99iBbyeHypRi5DA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrgeekgdekiecutefuodetggdotefrodftvf 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 63504E4740; Thu, 17 Jan 2019 11:41:27 -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: <68ed1eaa-7a03-fbaf-26e2-a1979b636174@aklaver.com> Date: Thu, 17 Jan 2019 08:41:26 -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. Got to thinking more and realized the answer depends on what you want the query to produce. Can you let us know what is you are trying to pull out with the query? > > Looking forward to learning, > > Rich > > -- Adrian Klaver adrian.klaver@aklaver.com