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 1vRHe1-00AnE5-1p for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 22:17:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vRHe0-005FUA-2D for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 22:17:48 +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 1vRHe0-005FU2-14 for pgsql-general@lists.postgresql.org; Thu, 04 Dec 2025 22:17:48 +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 1vRHdy-003Amy-0g for pgsql-general@postgresql.org; Thu, 04 Dec 2025 22:17:47 +0000 Received: from salmo.appl-ecosys.com (salmo.appl-ecosys.com [192.168.55.1]) by mail.appl-ecosys.com (Postfix) with ESMTP id 078D12A14D6 for ; Thu, 4 Dec 2025 14:17:46 -0800 (PST) Date: Thu, 4 Dec 2025 14:17:45 -0800 (PST) From: Rich Shepard To: "pgsql-general@postgresql.org" Subject: Re: Extract only maximum date from column In-Reply-To: <722e065d-8c21-4012-9752-b43658a99c9c@aklaver.com> Message-ID: References: <54638724-615f-52f-29a6-74e2567b9599@appl-ecosys.com> <722e065d-8c21-4012-9752-b43658a99c9c@aklaver.com> 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, Adrian Klaver wrote: > 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; Adrian, Reformated and still has an error: 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; psql:companies-contacted-2025.sql:16: ERROR: missing FROM-clause entry for table "c" LINE 3: group by c.person_nbr) So, tweaking from reported errors: WITH lc AS (SELECT p.person_nbr, max(c.next_contact) AS last_contact from people as p, contacts as c where next_contact >= '2025-11-01' group by p.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; psql:companies-contacted-2025.sql:9: ERROR: missing FROM-clause entry for table "person" LINE 7: join lc on p.person.nbr = lc.person_nbr; ^ This is obviously a much more complicated query than I expected. Thanks, Rich