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 1gu1bo-0007cN-DJ for pgsql-general@arkaria.postgresql.org; Wed, 13 Feb 2019 20:58:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gu1bm-0000uX-Pr for pgsql-general@arkaria.postgresql.org; Wed, 13 Feb 2019 20:58:18 +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 1gu1bm-0000uQ-EN for pgsql-general@lists.postgresql.org; Wed, 13 Feb 2019 20:58:18 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gu1bf-0001Vp-Sm for pgsql-general@lists.postgresql.org; Wed, 13 Feb 2019 20:58:17 +0000 Received: from [192.168.127.1] (port=24015 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gu1bc-000L3J-TD; Wed, 13 Feb 2019 20:58:08 +0000 Received: from [127.0.0.1] (port=30936 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gu1bc-0006Yp-8G; Wed, 13 Feb 2019 20:58:08 +0000 From: Andrew Gierth To: Adrian Klaver Cc: Rich Shepard , "pgsql-generallists.postgresql.org" Subject: Re: Subquery to select max(date) value In-Reply-To: <7c015580-fe20-be68-076c-ded42d8bf1f1@aklaver.com> (Adrian Klaver's message of "Wed, 13 Feb 2019 12:25:55 -0800") Message-ID: <875ztno1l6.fsf@news-spur.riddles.org.uk> References: <2780074d-3b87-b154-8bc6-a0f898fbdde2@openvistas.net> <7c015580-fe20-be68-076c-ded42d8bf1f1@aklaver.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Wed, 13 Feb 2019 20:58:08 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Adrian" == Adrian Klaver writes: 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.: Adrian> select Adrian> p.person_id, Adrian> p.desc_fld, Adrian> a.next_contact Adrian> from Adrian> people as p Adrian> LEFT JOIN ( Adrian> SELECT Adrian> DISTINCT ON (person_id) [...] Adrian> ) a USING (person_id) Adrian> ; 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: select p.*, a.* -- for illustration from people p join lateral (select * from activities a1 where a1.person_id = p.person_id and a1.next_contact > '2018-12-31' and a1.next_contact <= 'today' order by a1.next_contact desc limit 1) a on true; (make sure to have an index on activities(person_id,next_contact)) -- Andrew (irc:RhodiumToad)