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 1gu16e-0004oe-Jy for pgsql-general@arkaria.postgresql.org; Wed, 13 Feb 2019 20:26:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gu16d-0000W8-AL for pgsql-general@arkaria.postgresql.org; Wed, 13 Feb 2019 20:26:07 +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 1gu16c-0000W0-AQ for pgsql-general@lists.postgresql.org; Wed, 13 Feb 2019 20:26:07 +0000 Received: from wout2-smtp.messagingengine.com ([64.147.123.25]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gu16X-00040Q-DP for pgsql-general@lists.postgresql.org; Wed, 13 Feb 2019 20:26:05 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 4409434AC; Wed, 13 Feb 2019 15:25:58 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 13 Feb 2019 15:25:58 -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=n CAwFVYzI5W7Z/u5MgsT+qzcsof875i4QgoLWugxuJ4=; b=W+n9HvwKvgqygjcQv sFIu1e9MTwTDDcrIh1BGBo7GeMlgXtc4/u4OsHJTFO3hYoi9WvdAiSC9SeoPMYU0 eBjDjga72pbrf7gHoLk9hRK1A4+mxoHn09LULD9RDF7Yx9V1LG05loKdjyYUt7Nw xAEGpNBEeNe+BiX+JZLkiEzl3BaQk/j+iejf+Ef374PBXFjKsJcwY2S/15OrCwyy 1jPhKP7PEakGwak9aqBmmJP8kpSdZaNDS/I0c3cHwIRmpIGg1CfSc2wh+l3JBTTM /8dn9XWd7u7bkYWDR/efIxBMeMuczIH3zXMoj2tSzbtvCo/9Xk8QnPKaN8rB5T4/ sSkmA== 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=nCAwFVYzI5W7Z/u5MgsT+qzcsof875i4QgoLWugxu J4=; b=irSBFXvPZbkzqG/KNjHiimQFE011EBknmn9PTwpj0k9us+TwstmG3o7Kz PrSasmttXXiEKU8qHU1lE3c6LhE2kxCOr7ScQuRLRf1bG+0BG+XVodBs1/DDVjsc TdW+sGBrCXxJj2s89GXAm3NJQ4yzszpaBaoDeD1qgNfT3x60sLTPZF/kjfsazYP3 ynb4c7P/odn0djaSHHw5s6d3sS/AyeDOlc/ovIf4cdYGVN6mzXHk/Z2GrDSndgB2 u6AIifD+Tjmla67BXVFM2XJGzTDFF1I2gAdkvCz7P7U6r4prxhMBuNcKeGxGGP5P Y6cX4MNIEpp2Zlf8OjRf0dfTiRNzA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledruddtfedgudefkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthenuceurghilhhouhhtmecu fedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurhepuffvfhfhkf fffgggjggtgfesthejredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcu oegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucfkphepjedurd dvuddvrdduhedtrddvtddvnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id D4EAC1031C; Wed, 13 Feb 2019 15:25:56 -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: <7c015580-fe20-be68-076c-ded42d8bf1f1@aklaver.com> Date: Wed, 13 Feb 2019 12:25:55 -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 > and learn where to specify no nulls. create table people(person_id integer, desc_fld varchar); create table activities(person_id integer, next_contact date); insert into people values (1, 'contacted'), (2, 'never contacted'), (3, 'out of range'), (4, 'contacted'); insert into activities values (1, '01/31/19'), (3, '11/01/18'), (4, '02/02/19'); No activities record for person_id =2, activities data set to NULL: SELECT * FROM people AS p LEFT JOIN activities AS a ON p.person_id = a.person_id; person_id | desc_fld | person_id | next_contact -----------+-----------------+-----------+-------------- 1 | contacted | 1 | 2019-01-31 2 | never contacted | NULL | NULL 3 | out of range | 3 | 2018-11-01 4 | contacted | 4 | 2019-02-02 Close to your last posted query. person_id 2 and 3 have NULL values for activities data as there is no record for 2 and 3 is out of the date range.: select p.person_id, p.desc_fld, a.next_contact from people as p LEFT JOIN ( SELECT DISTINCT ON (person_id) person_id, next_contact FROM activities a WHERE a.next_contact <= 'today' and a.next_contact > '2018-12-31' and a.next_contact is not null ) a USING (person_id) ; person_id | desc_fld | next_contact -----------+-----------------+-------------- 1 | contacted | 2019-01-31 2 | never contacted | NULL 3 | out of range | NULL 4 | contacted | 2019-02-02 (4 rows) What I think you want: select p.person_id, p.desc_fld, a.next_contact from people as p LEFT JOIN ( SELECT DISTINCT ON (person_id) person_id, next_contact FROM activities a WHERE a.next_contact <= 'today' and a.next_contact > '2018-12-31' and a.next_contact is not null ) a USING (person_id) WHERE next_contact is not null; person_id | desc_fld | next_contact -----------+-----------+-------------- 1 | contacted | 2019-01-31 4 | contacted | 2019-02-02 > > Thanks, > > Rich > > -- Adrian Klaver adrian.klaver@aklaver.com