Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vRH3S-00AhL2-0f for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 21:40:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vRH3P-0051I5-2G for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 21:40:00 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vRH3P-0051Hv-1D for pgsql-general@lists.postgresql.org; Thu, 04 Dec 2025 21:39:59 +0000 Received: from mail.appl-ecosys.com ([50.126.108.78]) by makus.postgresql.org with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vRH3M-003AWh-35 for pgsql-general@postgresql.org; Thu, 04 Dec 2025 21:39:58 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id 768C72A14D6 for ; Thu, 4 Dec 2025 13:39:56 -0800 (PST) Date: Thu, 4 Dec 2025 13:39:56 -0800 (PST) From: Rich Shepard To: "pgsql-general@postgresql.org" Subject: Re: Extract only maximum date from column In-Reply-To: Message-ID: <54638724-615f-52f-29a6-74e2567b9599@appl-ecosys.com> References: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII; format=flowed List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 4 Dec 2025, David G. Johnston wrote: > I would go with a lateral join subquery of the contracts table. Using an > aggregates to perform ranking is an anti-pattern. You want the contract > ranked first when ordered by contract_date. Either use a window function > to explicitly rank the contracts or use a limit/fetch clause to simply > return the first ordered one. David, I'm closer, but still missing the proper syntax: select p.person_nbr, p.company_nbr, c.next_contact from people as p, contacts as c join lateral (select max(c.next_contact) as last_contact where p.person_nbr = c.person_nbr and last_contact >= '2025-11-01' ) c on true; resulting in: psql:companies-contacted-2025.sql:9: ERROR: aggregate functions are not allowed in FROM clause of their own query level LINE 3: join lateral (select max(c.next_contact) as last_contact Regards, Rich