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 1gtvpl-0007P7-CT for pgsql-general@arkaria.postgresql.org; Wed, 13 Feb 2019 14:48:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gtvpk-0008QZ-70 for pgsql-general@arkaria.postgresql.org; Wed, 13 Feb 2019 14:48:20 +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 1gtvpi-0008Iu-0f for pgsql-general@lists.postgresql.org; Wed, 13 Feb 2019 14:48:19 +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 1gtvpe-0001RZ-Ki for pgsql-general@lists.postgresql.org; Wed, 13 Feb 2019 14:48:16 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 220F43556; Wed, 13 Feb 2019 09:48:13 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 13 Feb 2019 09:48:13 -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=H oTzzZTI3ZU1RckA6aGGeeI3RAL1H2LjgI3xNVd8Xpw=; b=E/IJpkr0mZZYjsa20 JNxYNZKpjU+8Y2W7jPJZyQTSuCXSLbMhbmdLnixwXUJTCvCViya5fjrivsGEyisL uDXukmdBI05Z1Sjo+tyQYnCo2tZMit1foErmRxFq4qbfY/bZ/5nCGj6FG8Wmo2iA 9wVU4y4u2qMuZ+CAFN3naYzmaVwyAMlPnT3ai1qMvQ9PDOm/wxII2CzgV880JTif fntxEzw76BIpitIhwY06O8NuIbrAqLscqu3fvSL1mBLLwh7c7dtnt3NcGEPi/3rh 5m5L1WTVJG/o1pzoh9WqqWpVthBnYrIJhMvonnZ7YMLox8R0nb9/pknzjTtq/Hhq 1DQiw== 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=HoTzzZTI3ZU1RckA6aGGeeI3RAL1H2LjgI3xNVd8X pw=; b=IddYgxkgpTUtIuuRkiiUDh8CEzcL+Gcr06R1F4AH8mRC8QWWOKxp0eDp0 0uzPItD1ldN92EVw73kSJvSMnwqr8sRbrg6WAC4p/7+7krU6Kl+DOh8+F7MRHQYb aJC6oDMdHwua4VO083tGwcsgyay/q4cYWOIuHTeCWNl/k2eFF5lq78CSj11wfMeu 4VTkNoBc2sTgIrr5R31w9SxnnJfVRZRgOQ5To0elo/Qww/1PlMm0MTRNKhVafElf ZTpDm1XeHlQ1P5DflL0RyStO/gm03JbQwFbxwNkqblii7zEhZLlYZpgfZSQJlDHQ n/zRm4UXPQV8bfTmkE0jQkWEwq+FA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledruddtfedgieelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhhtnecuuegrihhlohhuthemucef tddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefuvfhfhffkff gfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceo rggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecukfhppeejuddrvd duvddrudehtddrvddtvdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id 926D4E4412; Wed, 13 Feb 2019 09:48:11 -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 06:48:10 -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 2/13/19 6:28 AM, Rich Shepard wrote: > On Tue, 12 Feb 2019, Ken Tanzer wrote: > >> If that's getting you what you want, then great and more power to >> you.  It >> looks like you'll only get people who have a next_contact in your target >> window there.  You might also consider something like this... >> >> select >>    p.person_id, >>    p.lname, >>    p.fname, >>    p.direct_phone, >>    o.org_name, >>    a.next_contact >> from >>    people as p >>    LEFT JOIN organizations o USING (person_id) >>    LEFT JOIN ( >>        SELECT >>            DISTINCT ON (person_id) >>            person_id, >>            next_contact >>        FROM activities a >>        -- WHERE ??? >>        ORDER BY person_id,next_contact DESC >>    ) a USING (person_id) >> ; > > I modified this to restrict the time and ignore nulls by replacing the > question marks: > > WHERE a.next_contact <= 'today' and a.next_contact > '2018-12-31' and >       a.next_contact is not null > > For a reason I've not yet found, the last condition is not observed; i.e., > those rows with null next_contact dates appear in the results. Position in > the sequence makes no difference. What might cause this? 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. > > Regards, > > Rich > > > > -- Adrian Klaver adrian.klaver@aklaver.com