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 1vRHZe-00AmTl-0S for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 22:13:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vRHZb-005CSL-2v for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 22:13:16 +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 1vRHZb-005CSD-1e for pgsql-general@lists.postgresql.org; Thu, 04 Dec 2025 22:13:15 +0000 Received: from mail-oo1-xc34.google.com ([2607:f8b0:4864:20::c34]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vRHZZ-003Akt-1M for pgsql-general@postgresql.org; Thu, 04 Dec 2025 22:13:14 +0000 Received: by mail-oo1-xc34.google.com with SMTP id 006d021491bc7-6597046fc87so522300eaf.0 for ; Thu, 04 Dec 2025 14:13:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764886393; x=1765491193; darn=postgresql.org; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=4J6yuduTFZtXx+sm07okPIXEd8R8j7EYzUBxUEH4Ei8=; b=SSvpRZecsKwT7YK81hyan/3vsxesGBVVqhmiAYzYpHJRfPbvfNbJofP91NvIIpm5FB BVbxM8t/AGFM8ELxjzsE92I3Mx5DNf0hcd1+N5Z/xXEurg2lCMxls28feP4qLLeGYq5A KPr4UYieYPfmasMQQM4rVqb9f5cxbUrUvBMPmdx4f9GmIncY27E24dM5hbF7iiGeqxNX DXWtS9wbhhJm34MSkTdgbGCt7R93iSNAEAMk1zoyKanSM44m8NSpKt43ZpVCRSgIzybS zpDs55Hg/+Q3GZnR0CR3rmw4ySsTqhvxITLYjN4W4+qZ0dA7rHu5L+rxFgVHzKOl3026 FgPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764886393; x=1765491193; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=4J6yuduTFZtXx+sm07okPIXEd8R8j7EYzUBxUEH4Ei8=; b=dpsLjiOLy3YVVx/++Jda0lJCcmZVolwowacY2c0U6L5mDaZxV73xtcaUnp1MmcT8HQ L7opPzaS6GYQDfzBA83hFE56NFZCgIhUIx8GEy0wH4ckItzPssGGQmBkja9aiZDELvoQ rCFwWhrcQZ5l/qOnRWwg+FbNiKwIGyA87L6RVXOhkjgWrrJ3KD01xtOCgIbLuFiTVWwz 99NmAd2AhfiY5ErBcWs2m2w6ncJkE1OPQllFO5YEbUI/l88Uzr64nsme0KzD/dedjunV N8B16dqGY4FRelVZKTH72ujcOvrr5IjqAQxoG82N+RmEWS2iwfF6le0tTlsLd9AsU7oK mNqA== X-Gm-Message-State: AOJu0YzPA+r2eYRtk+mJjnbmGTMSE/6irSgV6tyIirJ9J5RN8J9xNRvn +/UOZ01St9SgFaUYedhNIjC8zxCgQlcyNvV5hGM3QaXS0E79BaqDKB6XqFHNbmGRVy5CBN53TrJ YCwZftIu8+Dghnu2NwHvmBasA2inQQF8lMA== X-Gm-Gg: ASbGncta+uznXas/E5RRChltEadsxvG+SYRHfZdYjujz5mvv/rEk9XxHOEueotpscm9 6i5jIh6g5BR9lDBIJS96POwzyZ91jgUMqZxRyNYDK04AGiyOShngMsn3GDgzPVjwruHCVJjpOhs sbdRmxEAhWW80rAVwsVrPG1UDmfP4A9nYIZcuD71A77whfhlFO6JIHfKbqgd2kirU6CDtHFe2+y rhTnD6Ru6gJVuf+l5CqICKc2P77jtD1W6kKx/z2qSRZg9mzVSAu++Mk+s971gWFHyZegw== X-Google-Smtp-Source: AGHT+IFqLcPvamcLoi1F0h0WRVH47s72J6V5IxVSavqRnEdwDuQVXeqX62GsLm8Y+dJv2XLvho+CXUUhSfdOdNAn7XI= X-Received: by 2002:a05:6820:999:b0:659:872e:7664 with SMTP id 006d021491bc7-659872e7ac1mr42327eaf.7.1764886392686; Thu, 04 Dec 2025 14:13:12 -0800 (PST) MIME-Version: 1.0 Received: by 2002:a05:6802:4982:20b0:5f5:300f:8355 with HTTP; Thu, 4 Dec 2025 14:13:11 -0800 (PST) In-Reply-To: <54638724-615f-52f-29a6-74e2567b9599@appl-ecosys.com> References: <54638724-615f-52f-29a6-74e2567b9599@appl-ecosys.com> From: "David G. Johnston" Date: Thu, 4 Dec 2025 15:13:11 -0700 X-Gm-Features: AWmQ_bkQACcLVfs4JXzmM9-ryJ23V-9R71D19CGmi0mAM9fOuvsfDm0WQAprM3o Message-ID: Subject: Re: Extract only maximum date from column To: Rich Shepard Cc: "pgsql-general@postgresql.org" Content-Type: multipart/alternative; boundary="000000000000814c2c064527a2a6" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000814c2c064527a2a6 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thursday, December 4, 2025, 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 =3D c.person_nbr and > last_contact >=3D '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 > As mentioned, the aggregate max should be avoided - you aren=E2=80=99t doin= g statistics, you are ranking. Select person.*, lastcontact.* from person join lateral (select contact.* from contact where contact.person_id=3Dperson.person_id order by last_contact_date desc limit 1) as lastcontact on true; David J. --000000000000814c2c064527a2a6 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thursday, December 4, 2025, Rich Shepard <rshepard@appl-ecosys.com> 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
=C2=A0 =C2=A0 =C2=A0where p.person_nbr =3D c.person_nbr and
=C2=A0 =C2=A0 =C2=A0last_contact >=3D '2025-11-01'
=C2=A0 =C2=A0 =C2=A0)
=C2=A0 =C2=A0 =C2=A0c on true;

resulting in:
psql:companies-contacted-2025.sql:9: ERROR:=C2=A0 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

As mentioned, the aggregate max should be = avoided - you aren=E2=80=99t doing statistics, you are ranking.
<= br>
Select person.*, lastcontact.* from person join lateral (sele= ct contact.* from contact where contact.person_id=3Dperson.person_id =C2=A0= order by last_contact_date desc limit 1) as lastcontact on true;
=
David J.

--000000000000814c2c064527a2a6--