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 1vRHL1-00AkPo-2r for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 21:58:12 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vRHL0-0057D6-2u for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 21:58:11 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vRHL0-0057Cy-0A for pgsql-general@lists.postgresql.org; Thu, 04 Dec 2025 21:58:10 +0000 Received: from fhigh-b3-smtp.messagingengine.com ([202.12.124.154]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vRHKx-003AsJ-25 for pgsql-general@postgresql.org; Thu, 04 Dec 2025 21:58:10 +0000 Received: from phl-compute-03.internal (phl-compute-03.internal [10.202.2.43]) by mailfhigh.stl.internal (Postfix) with ESMTP id B9BE67A01E7; Thu, 4 Dec 2025 16:58:03 -0500 (EST) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-03.internal (MEProxy); Thu, 04 Dec 2025 16:58:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1764885483; x=1764971883; bh=WCkJKGH7FUlrJQ8TjdTJK8jpg9FIEmstL7Q+PFMAAgc=; b= rKWSdmPNylczilVmxF7OMzyxxO/H8syj50jgAWwvE5yFaCUhb+Pra47lDdwIeZ+l gIP3/ft4C5h9Ldl0uo2F5vL+M64O9LB1+MH/sZmIk7v99RH/MSoWWfRhnDtnaNZk yqyf5Se0Qfk9Xx4fHg6GBOXd/Dpul9+jhaCec+w+Sojg5lnoU9by+1QTxkkq9XmW hoccLvgVwAxmuGKL7Tsy122iCOzHTmPiAumRxwCPp9KopypSJgHBzDaIxu3Qa0bi cieY4TI9offs5yIzfdwwKbAL5vG3vXYMOMjck08+PGSjfzfr9oxrYTlRg2Cg/qdA OZc7mMAUeVTx5TxnweDk2A== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1764885483; x=1764971883; bh=W CkJKGH7FUlrJQ8TjdTJK8jpg9FIEmstL7Q+PFMAAgc=; b=ldbjb0CHfk/FZKkMt VYRCwgWSPOZCV/JGJmdajHVB7h08Z3NY0VQ1gU8MziUEvM9ZRTJJE+UtJA72kUdN hZpBCS3MLj8okXf0YFfDk8/dpjqEk23aWv+GQHrRtxxyTOyY914k7yQkONm1mx+t wZPZ8cnD/iu4fQV1b7wRuzT7vKwE8TYhODx6+Qj7TvOnfcidX0wbHNR79vFnLrsH gADhowZki3U3GGi3KpvKed1M05RetQRszNbPrbkiCowi9D4Q3xJxUSQH7pSU2a5p Ic9+Sfl5+8DS14qAhkCYhoecLJc1lo4wKl/UZBwkCx041dUzzQUoE/zqYaPT9HRZ vk6bQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefgedrtddtgdeijeduucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceurghi lhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmnecujfgurh epkfffgggfuffvfhfhjggtgfesthekredttddvjeenucfhrhhomheptegurhhirghnucfm lhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenuc ggtffrrghtthgvrhhnpeffleegieefgfevudehtdfhkeeutdffjeevgeffgeejvedthefg udeiteefheejheenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfh hrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmpdhnsggprhgt phhtthhopedvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehrshhhvghprghrug esrghpphhlqdgvtghoshihshdrtghomhdprhgtphhtthhopehpghhsqhhlqdhgvghnvghr rghlsehpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 4 Dec 2025 16:58:02 -0500 (EST) Message-ID: <722e065d-8c21-4012-9752-b43658a99c9c@aklaver.com> Date: Thu, 4 Dec 2025 13:58:01 -0800 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Extract only maximum date from column To: Rich Shepard , "pgsql-general@postgresql.org" References: <54638724-615f-52f-29a6-74e2567b9599@appl-ecosys.com> Content-Language: en-US From: Adrian Klaver In-Reply-To: <54638724-615f-52f-29a6-74e2567b9599@appl-ecosys.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 12/4/25 1:39 PM, Rich Shepard wrote: > 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 Would the below work?: WITH lc AS (SELECT person_nbr, max(next_contact) AS last_contact from contacts where next_contact > '2025-11-01' group by c.person_nbr) select p.person_nbr, p.company_nbr, lc.last_contact from people AS p join lc on p.person.nbr = lc.person_nbr; > > Regards, > > Rich > > -- Adrian Klaver adrian.klaver@aklaver.com