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 1gkHY9-00071a-LF for pgsql-general@arkaria.postgresql.org; Thu, 17 Jan 2019 23:58:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gkHY8-0008BH-Fn for pgsql-general@arkaria.postgresql.org; Thu, 17 Jan 2019 23:58:16 +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 1gkHY6-0008AR-FX for pgsql-general@lists.postgresql.org; Thu, 17 Jan 2019 23:58:16 +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 1gkHY2-0004V3-ON for pgsql-general@lists.postgresql.org; Thu, 17 Jan 2019 23:58:13 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 93F6228B5E; Thu, 17 Jan 2019 18:58:09 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Thu, 17 Jan 2019 18:58:09 -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=q dCQCjlXUNiDkzVQk1YvCaJURTfg9idGYKONwPSiHt0=; b=HwDzhiVjmpm26oGzY FvO+edK9nKE4CKloIS2aX89pSZ27+Hf98Yz7irnvY7jUz5uxFGkwrwwLAEgXNS3P 13cRSQk+X4IBZwVy9SmlyzMcqelGYaMREwc3lVxae7Ot3raBaljvEpj6vnEpAblJ g3Jr44+eMQFqdcQqHmMRaYcKjLyKJYuzdFKRFVljCS64QKv1lzYHdLOs7HBmd8q9 l3lIvbbxn4iJ7GK/F7lpXtZacm+jpoSTKXsgVk3FCjJ8HHFJ+TimG5y3pCeuySb0 4prkV8wEE3E9+W3eH6ZbK4dCWVh7cMPSvzchdCebCs0ce+OM2Pdzm6lvD+ZdexMK JYEYQ== 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=qdCQCjlXUNiDkzVQk1YvCaJURTfg9idGYKONwPSiH t0=; b=ND0tcmHjrkETZ7/FkXVXEwAOYp145+DC6A8nWkkbUV984WmJO1GiL3f4J Nss5WnnuEWSTCOVnaNZ3HVBP4EtqoDvrad4I2u6HI0mz+G93BUVsueBjbmCNbpdN /2mxSqcXqmTVYybSOy6OJfakyZanfsPOvios6jCXw58VWgNgcSCVb1Eotu6V80U7 V0bLy01UXoyo88l52kWF9mhQY1+faOXj7amlQlSrbfV+ZoqeTZ5ZFk5SiLi89ERQ AcLQLCvilg/xyZAJNJdEDuAO5WJYcxs/uU0BU0m3191HV2SXARZLWQtusYGmi1n/ xeC+t02m07J8M3w32MPGp0vgjD7MA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrgeelgddufecutefuodetggdotefrodftvf 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 D878A102A0; Thu, 17 Jan 2019 18:58:08 -0500 (EST) Subject: Re: Refining query statement To: Rich Shepard , pgsql-general@lists.postgresql.org References: <9a5976c8-76da-f06d-9840-5dcf8723e38b@gmx.net> <68ed1eaa-7a03-fbaf-26e2-a1979b636174@aklaver.com> From: Adrian Klaver Message-ID: Date: Thu, 17 Jan 2019 15:58:07 -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 2:44 PM, Rich Shepard wrote: > On Thu, 17 Jan 2019, Adrian Klaver wrote: > >> 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? > > Adrian, et al., > > Took your advice and re-thought what I need the query to return. This > allowed me to realize that I don't need a separate contact history query as > I can incorporate it in a single query. The goal and pseudocode are: > > Purpose: List all contact information and contact history for active people > where next contact date is less than today. > > For each person select person_id, lname, fname, and direct_phone from > People. > > For each person get the org_name from Organizations. > > For each person get contact history in date order and next contact date > from > Contacts where active = True. > > Order by next contact dates in ascending order. > > Query code: > > SELECT p.person_id, p.lname, p.fname, p.direct_phone, o.org_name, > c.cont_date, >        c.cont_type, c.notes, c.next_contact, c.comment > FROM People AS p >      JOIN Organizations AS o ON p.org_id = o.org_id >      JOIN Contacts AS c ON c.person_id = p.person_id > WHERE c.active = TRUE AND c.next_contact <= 'today'::date > GROUP BY o.org_name, p.person_id, c.person_id, c.cont_date, c.cont_type, >       c.next_contact > ORDER BY p.person_id, c.next_contact > > The results are not correctly grouped or ordered; I'll work on fixing these > issues. > > The other issue that needs fixing is identifying the most recent 'active' > value in the Contacts table for each person_id and including that person in > the results only when active = 't'. Here's a MWE of a redacted set of rows > that should not have been returned by the query: > >  person_id | cont_date  | cont_type  | >                              notes >                           | active | next_contact | comment > --------------------------+--------+--------------+--------- >         36 | 2018-12-12 | Conference | Notes on this conversation. >                         | t      | 2018-12-17   | >         36 | 2018-12-17 | Phone      | Left message asking for a meeting. >                           | t      | 2019-01-03   | >         36 | 2019-01-03 | Phone      | Left another message. >                           | t      | 2019-01-07   | >         36 | 2019-01-07 | Phone      | Going into a meeting. >                           | t      | 2019-01-14   | >         36 | 2019-01-15 | Phone      | Will call when/if. >                           | f      | infinity     | (5 rows) > > Because the most recent row's active value is 'f' these rows should not be > included as there is no next contact date. I'll keep reading looking for > this answer and pointers will be helpful. I would think the active would be on People or Organizations. Then you can eliminate then from the query results before you ever got to the contact history. Also not sure how you the above result when the query showed: WHERE c.active = TRUE AND c.next_contact <= 'today'::date Or did the history results come from an undisclosed query? > > Regards, > > Rich > > -- Adrian Klaver adrian.klaver@aklaver.com