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 1gu3OM-0000Zd-9P for pgsql-general@arkaria.postgresql.org; Wed, 13 Feb 2019 22:52:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gu3OK-0007bK-Sm for pgsql-general@arkaria.postgresql.org; Wed, 13 Feb 2019 22:52:32 +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 1gu3OI-0007bB-Tw for pgsql-general@lists.postgresql.org; Wed, 13 Feb 2019 22:52:32 +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 1gu3OF-0007LH-0L for pgsql-general@lists.postgresql.org; Wed, 13 Feb 2019 22:52:30 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.west.internal (Postfix) with ESMTP id 6F5ED3330; Wed, 13 Feb 2019 17:52:24 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Wed, 13 Feb 2019 17:52:24 -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=Y OTabq2A2UEYvJoQgp2g7DPyD7GECLNd6Whv1ttdoRs=; b=FQzGKQnq7Y0e8q2vK K2gqG/zVSPlOX9e3nlXVMG5O8p92Vehok3FeQOGJPQD+nOCCZXCBR4ocsylXA6a+ /rOSaYLNDoCRVCa1TN1V6Qb3nm/I9jwWyAj/saf6EEAKJnGPgZvGz6OGv0qUJfdJ V4h/F8pmItAsjEk8e3JOwMGW18OVgHioIssCaXo2oQb848I+NeoaJzJlbjzCiYbb gZScNz3EJKB6UXgOkH+0cry5lenQPgFqzqNuGsX6n3Jf4EeMeyNXPd9z+YV95XDO 0h8s/n5yBqdGPEh0ZnfVkObGZm+62CUX3/kMpRk9hOKIrgcctMgDaafCo8yWgKpB DvNYw== 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=YOTabq2A2UEYvJoQgp2g7DPyD7GECLNd6Whv1ttdo Rs=; b=T1aQb9dr4SPujczsCxtKbNmq8yXu8FcKrIX23krXBnTfPZYQUSjWUSvU/ lp54zjHhIrTKNPUm5yKN2SVYGbOCCxI+qRhLAUO0adm439H999sv+FStbGbP+2Hv NHIOVswKBKwlm4vZ8IpNvfDUFi3Vrv1QSeDR0oENXF6wugs2vzhuHNYZ88AQv4qa j4BvLHpRBcSshXZwTxalVdSl6N9EBzdSY2aul03Eh/Pnq+OSfVzdKpO9/29NSbYO mp/jIXWhJTAIt9ahc+hCfBstXQ5YXzvxv4evH6D3hh/6Ga4moSn6thvbuO7Who96 hx/rYLFGSCV7XcMVav9x4MYZlzY6w== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledruddtgedgtdduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhhtnecuuegrihhlohhuthemucef tddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefuvfhfhffkff gfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceo 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 6DB9110315; Wed, 13 Feb 2019 17:52:23 -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> <7c015580-fe20-be68-076c-ded42d8bf1f1@aklaver.com> <875ztno1l6.fsf@news-spur.riddles.org.uk> From: Adrian Klaver Message-ID: <0dee994f-120d-06b6-4aea-a10060d2687c@aklaver.com> Date: Wed, 13 Feb 2019 14:52:22 -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 2:24 PM, Rich Shepard wrote: > On Wed, 13 Feb 2019, Andrew Gierth wrote: > >> Adrian> Close to your last posted query. person_id 2 and 3 have NULL >> Adrian> values for activities data as there is no record for 2 and 3 is >> Adrian> out of the date range.: > >> DISTINCT ON with no matching ORDER BY at the _same_ query level is >> non-deterministic. >> >> Also DISTINCT ON isn't efficient. Consider instead something along the >> lines of: > > Andrew/Adrian, > > I again read about DISTINCT and DISTINCT ON and fully understand them. I've > also again read about JOINs; I understand them in terms of sets and _think_ > that in this query the people table is the LEFT (many) while the > organizations and activities tables are the RIGHT (one) in the many-to-one > relationships. That is, for each person_id there is only one org_id and > only > one next_contact that meets the three constraints. Given a sufficiently large date range that may not be true as you may have contacted a given person multiple times during that range and generated multiple activities records. > > I'm now working on understanding how the syntax in the examples you two, > Ken, and others have provided expresses the many-to-one relationships of > organization and activities to people. I have the syntax that returns the > next_date meeting the WHERE constraints to each person_id and am now > focusing on adding the additional people and organization columns to the > results. Might not be until tomorrow or Friday but I'll let you and the > list > subscribes know when I have understood all your suggestions and get the > results I want from the query. > > Thanks again, > > Rich > > > -- Adrian Klaver adrian.klaver@aklaver.com