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 1gtwkN-0004Mi-De for pgsql-general@arkaria.postgresql.org; Wed, 13 Feb 2019 15:46:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gtwkL-0004xg-DN for pgsql-general@arkaria.postgresql.org; Wed, 13 Feb 2019 15:46:49 +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 1gtwkL-0004xZ-26 for pgsql-general@lists.postgresql.org; Wed, 13 Feb 2019 15:46:49 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gtwkI-0002pF-5e for pgsql-general@lists.postgresql.org; Wed, 13 Feb 2019 15:46:47 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id EBA463525; Wed, 13 Feb 2019 10:46:44 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 13 Feb 2019 10:46:45 -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=T 9LizkLubMkvGfIAdj8MgN54NjavAmJjpdLhJku54Dg=; b=BIVlf+8ronLHruHcC DZDYPbNlUDfSfUfOXvVx877qa1QHaHRP3ag6Stf+HBzuYRENeAFThrlQuaCL0MvS 5ANKd1R3aSZzDIzFYcC4TeZYjUyITArF9pcd6yQhrtXm9lQv8zgUbbB1fhpXD5Rf hPWYZqQNyp+5ym2GIKISU+1PTKMm6qiKEA44pegJObpcHbm5Wocng0LpWB/wDLol nhU+4vr3TSFkjW7yRWKosYizcgrMBz7s/R0JzCDbREKM9r+cqPOpZWvP4gyEYiIT sSIlrgxCalj8ZzN2o27We9bb3jrkMtRXiYu+wUpykbZrFmq0OkMZMnZJRCfBeEUT kA/yw== 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=fm2; bh=T9LizkLubMkvGfIAdj8MgN54NjavAmJjpdLhJku54 Dg=; b=Qe6GI3P3Hdy3zbalcrbiquIWgiYMwYHVYd2x2od9jNMNi6aOgx4C+TEUn 4SqongPWWuo/G0cXONY0fGjARKjbA9504Psmx8l0XtZAdB6rdlM0HCZTCk9AjKKN 2/KfKJIg+GDcjukIWe8Unt0+HSLILEYj7OFLzGfpWGzSWIHEx4uXHHkbDSUb6mMr WJtRpUxmNt8VZhvm4IeT4A98wqyK0jM76icW3uw1baKOa04wl8jKScf6ezfZ9uuf r0WuQ8bZOujDV1eMHOdHARBgV010T5a065OS+CyFNXNPUsSSmE7D0dKFLZ56aMP5 ldQQWhybfsrMXJVflSI/nyB51LWTQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledruddtfedgkedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhhtnecuuegrihhlohhuthemucef tddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefuvfhfhffkff gfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceo rggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuffhomhgrihhnpe hpohhsthhgrhgvshhqlhdrohhrghenucfkphepjedurddvuddvrdduhedtrddvtddvnecu rfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvg hrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id ADF8BE4543; Wed, 13 Feb 2019 10:46:43 -0500 (EST) Subject: Re: Subquery to select max(date) value To: Rich Shepard , "pgsql-generallists.postgresql.org" References: <2780074d-3b87-b154-8bc6-a0f898fbdde2@openvistas.net> From: Adrian Klaver Message-ID: Date: Wed, 13 Feb 2019 07:46:42 -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: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2/13/19 7:37 AM, Rich Shepard wrote: > On Wed, 13 Feb 2019, Adrian Klaver wrote: > >> The LEFT JOIN. There are rows in people for which there no records coming >> from the sub-select on activities, so the row is 'padded' with NULL >> values >> for the missing data. > > Adrian, > > I assume it's the inner left join. I'll trace what's happening at each step AFAIK there is no inner left join: https://www.postgresql.org/docs/10/sql-select.html#SQL-FROM " join_type One of [ INNER ] JOIN LEFT [ OUTER ] JOIN RIGHT [ OUTER ] JOIN FULL [ OUTER ] JOIN CROSS JOIN " > and learn where to specify no nulls. You can't it is the nature of the join: "LEFT OUTER JOIN returns all rows in the qualified Cartesian product (i.e., all combined rows that pass its join condition), plus one copy of each row in the left-hand table for which there was no right-hand row that passed the join condition. This left-hand row is extended to the full width of the joined table by inserting null values for the right-hand columns. Note that only the JOIN clause's own condition is considered while deciding which rows have matches. Outer conditions are applied afterwards." What you are seeing are records for which there is a person in the people table that either: 1) Do not have a record under that person_id in the activities table or 2) Do not have a record that meets the date filtering you are doing on next_contact. > > Thanks, > > Rich > > -- Adrian Klaver adrian.klaver@aklaver.com