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 1vRHp0-00Aosu-1W for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 22:29:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vRHoz-005LMW-1k for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 22:29:09 +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 1vRHoz-005LMO-0f for pgsql-general@lists.postgresql.org; Thu, 04 Dec 2025 22:29:09 +0000 Received: from mail-oo1-xc2c.google.com ([2607:f8b0:4864:20::c2c]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vRHox-003B6F-1w for pgsql-general@postgresql.org; Thu, 04 Dec 2025 22:29:09 +0000 Received: by mail-oo1-xc2c.google.com with SMTP id 006d021491bc7-6596897c2b5so799260eaf.0 for ; Thu, 04 Dec 2025 14:29:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764887345; x=1765492145; 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=0Xl5Wd/qgSFx5Rf+e7wkjQNf3rMColLP2a8g9ebHTx0=; b=U7jNEgzJgz3eMgyi/lJiZN3LGHA7y0bvPaopO53x3fuEy7/iGzYJpefS58PEArVGXv rpOWZaiB4zY11F7rw3VsDefWTGcKcpar4kPSemZYVyCzl0+xa7vzFQ/x4IQaxXsS7ZSd o977miSsBXwNuTtdGeA30oSu+KiHiIWcO8FA0nrY/Ovt3gjPA5fM1IYXDn1Ma+XM0vFo UZzb/MH2wPCEZwTjl/pT9vVlUsafXY/fX3XWbMkurklQ8+0Vh+MkkbtrSs4EsVoN6q3U on7PJ0+5tBbpeangbJ7oEjCNbqtvwkOgVtUvKhxw/gFRkEMZMNUy9vlypJLsNkUCjxiV POmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764887345; x=1765492145; 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=0Xl5Wd/qgSFx5Rf+e7wkjQNf3rMColLP2a8g9ebHTx0=; b=nSMJvWNS00wAwo0QQ82rn/mnQh1mmkmiCclEhN3hwfWDQKEbLS/3Iv0NEU+ElnbKZR msL5nYyL1VfR/S78bRP0dF+BKiDo/L6RAGwyL93lwDHyV99+d0NBiRGCD0G3xcZ4+Ds5 qD8zibwpGXm9rVG/qLwli/cur+JSPL/TlaiPMGi/k8LaNf9VRK91r1jQKjDrazhemtLj lSj5mDQZXVEVJ709pd5iHUXyPwUk4umuvCP+q9VFvoKzEuSO2Q6D5deLtSHo7c57SNyH 9xMUFDqZCDW09ukariARtaIItmurpgtR5xD1etuhL8jaJ8Egu237rE0Z1lbeez/1rpW0 vU/Q== X-Gm-Message-State: AOJu0YybgeGCnN1mCXQ1Qih55dPli66n1RaT0GnSNcTfGzg03DfC44k1 lbW4ppatunHCuuEoJn55UYOzXNkc2UT6sXAdvGharKJbsboj0sAhainB2d22B4A23MvIBu6vnFE JWRlwXaZrViWMXyuA8W7QsmQLFy0o9fXYRA== X-Gm-Gg: ASbGnctekxchUszWSwcWMCKbSdOhmyxtEVAhhcjcAcFeL6xhtU0LI3ZMJRgI72BR9pe MwBnnwcHyjbeRFd/PdyVt4c3bFgNN4mxqpWKu7QVvrf2zWY12j0YifE0yJIL+FcTqZ6utJyzooy l8W2aB3U47qqgTDbSji2XOsIBdXh8dXYJ3jt89RLH/nmt+FaO2L52aAyjE5ryqyBd5/2fS5rTgg 3Qoz1qF0yE+kK3/XfhlkaoV8eyIM2dw3UisSalJikCA5eFtP6Dz6DOu7iDhL+8WNrGj68++zYsy Y6NX X-Google-Smtp-Source: AGHT+IHX3+LUFkY6m67HdAdvu8JbC33EoXpwX78xGAHnJ2nuiW9bw75/Vpvxu4TPvwKTVUpNGrzCXEFct/KGBnYVDBA= X-Received: by 2002:a05:6808:2899:b0:453:7cb1:871d with SMTP id 5614622812f47-4537cb191d6mr1720299b6e.52.1764887345321; Thu, 04 Dec 2025 14:29:05 -0800 (PST) MIME-Version: 1.0 Received: by 2002:a05:6802:3108:20b0:5e4:72ad:6236 with HTTP; Thu, 4 Dec 2025 14:29:04 -0800 (PST) In-Reply-To: <759515ff-7393-d28f-d627-6f4a48c11f4@appl-ecosys.com> References: <54638724-615f-52f-29a6-74e2567b9599@appl-ecosys.com> <759515ff-7393-d28f-d627-6f4a48c11f4@appl-ecosys.com> From: "David G. Johnston" Date: Thu, 4 Dec 2025 15:29:04 -0700 X-Gm-Features: AWmQ_bml0Tq3gjsS4F6K4cbEMm2zJ25mjFHgOravUB1XAoqgAEV0ZulEp3P_Krk Message-ID: Subject: Re: Extract only maximum date from column To: Rich Shepard Cc: "pgsql-general@postgresql.org" Content-Type: multipart/alternative; boundary="000000000000495a05064527dbf1" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000495a05064527dbf1 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: > > As mentioned, the aggregate max should be avoided - you aren=E2=80=99t do= ing >> statistics, you are ranking. >> > > David, > > Got it. > > 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; >> > > Select person.*, lastcontact.* > from people > join lateral (select contact.* > from contacts > where contacts.person_nbr =3D people.person_nbr > order by last_contact_date > desc limit 1) > as lastcontact on true; > > psql:companies-contacted-2025.sql:10: ERROR: missing FROM-clause entry > for table "contact" > LINE 3: join lateral (select contact.* > > So: > Select person.*, lastcontact.* > from people > join lateral (select contacts.* > from contacts > where contacts.person_nbr =3D people.person_nbr > order by last_contact_date > desc limit 1) > as lastcontact on true; > > psql:companies-contacted-2025.sql:10: ERROR: column "last_contact_date" > does not exist > LINE 6: order by last_contact_date > > I was giving you a query form. You should use the actual table and column names in your schema=E2=80=A6 David J. --000000000000495a05064527dbf1 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:

As mentioned, the aggregate max should be avoided - you aren=E2=80=99t doin= g
statistics, you are ranking.

David,

Got it.

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;

Select person.*, lastcontact.*
from people
join lateral (select contact.*
=C2=A0 =C2=A0 =C2=A0from contacts
=C2=A0 =C2=A0 =C2=A0where contacts.person_nbr =3D people.person_nbr
=C2=A0 =C2=A0 =C2=A0order by last_contact_date
=C2=A0 =C2=A0 =C2=A0desc limit 1)
as lastcontact on true;

psql:companies-contacted-2025.sql:10: ERROR:=C2=A0 missing FROM-clause= entry for table "contact"
LINE 3: join lateral (select contact.*

So:
Select person.*, lastcontact.*
from people
join lateral (select contacts.*
=C2=A0 =C2=A0 =C2=A0from contacts
=C2=A0 =C2=A0 =C2=A0where contacts.person_nbr =3D people.person_nbr
=C2=A0 =C2=A0 =C2=A0order by last_contact_date
=C2=A0 =C2=A0 =C2=A0desc limit 1)
as lastcontact on true;

psql:companies-contacted-2025.sql:10: ERROR:=C2=A0 column "last_c= ontact_date" does not exist
LINE 6:=C2=A0 =C2=A0 =C2=A0 order by last_contact_date
=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0

I was giving you a query form.=C2=A0 You s= hould use the actual table and column names in your schema=E2=80=A6

David J.
=C2=A0
--000000000000495a05064527dbf1--