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 1vRHm3-00AoUG-2J for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 22:26:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vRHm2-005IS2-1B for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 22:26:06 +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 1vRHm2-005IRu-08 for pgsql-general@lists.postgresql.org; Thu, 04 Dec 2025 22:26:06 +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 1vRHlz-003Axw-3C for pgsql-general@postgresql.org; Thu, 04 Dec 2025 22:26:05 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id C07D12A14D6 for ; Thu, 4 Dec 2025 14:26:03 -0800 (PST) Date: Thu, 4 Dec 2025 14:26:03 -0800 (PST) From: Rich Shepard To: "pgsql-general@postgresql.org" Subject: Re: Extract only maximum date from column In-Reply-To: Message-ID: <759515ff-7393-d28f-d627-6f4a48c11f4@appl-ecosys.com> References: <54638724-615f-52f-29a6-74e2567b9599@appl-ecosys.com> MIME-Version: 1.0 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 Thu, 4 Dec 2025, David G. Johnston wrote: > As mentioned, the aggregate max should be avoided - you aren’t doing > statistics, you are ranking. David, Got it. > Select person.*, lastcontact.* from person join lateral (select contact.* > from contact where contact.person_id=person.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 = 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 = 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 ^ Regards, Rich