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 1vRFYB-00AQjS-2R for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 20:03:40 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vRFYA-004YhX-2P for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 20:03:39 +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 1vRFYA-004YhP-1O for pgsql-general@lists.postgresql.org; Thu, 04 Dec 2025 20:03:38 +0000 Received: from mail-oo1-xc2b.google.com ([2607:f8b0:4864:20::c2b]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vRFY8-0039sA-10 for pgsql-general@postgresql.org; Thu, 04 Dec 2025 20:03:37 +0000 Received: by mail-oo1-xc2b.google.com with SMTP id 006d021491bc7-65968986a0cso711342eaf.3 for ; Thu, 04 Dec 2025 12:03:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764878616; x=1765483416; 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=MNCTjS79WSWyvXQ+p5caJdIYZRxQm7AUPHMSFl2EjQk=; b=LKEkgmjEi5blPCm/Qq1Fd8kclhrFOzdwolCJkr7bVSEmVMQq7bXOgd4u3DKPN8WPVr sMDWqbFoacEDw+k6mYeu8KJ3h0Kt3ln0Jq+tfF37svameGuV4asfUk9Yz5P5Jz3qdT1m 7/nRAIClmV66NuezXqy3JSRS44D6vSY104IbbYFPj6xAjeeuw/jCLTpkGWXiXsWO9Drv iCX8McKGw8rmWaLf/eiPdZ3ypKGXK1x4v+zRTLmP1L1Wr1pzoeIQPUgJdo5SLA1f2OXu kSTWoeLjBWYPnhyiJO9Kw4I8rf+dW/0q7S79BayLWTiEGD6M2Pw+2brpcBsz0CLzy0hU OZXQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764878616; x=1765483416; 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=MNCTjS79WSWyvXQ+p5caJdIYZRxQm7AUPHMSFl2EjQk=; b=wgvK+Onol1FvGizsbFCEbz0QIqjmHhUki9G62p6qvHcTf5WwCHxGh+fFJGPbs2BFD7 AFWTjkhkp9b/GTwpBYyAV2ELw5BM6OIUqe6Lip+COSsc0PguR7xL1c0k2QSU8+teG1cC gCrc/Pr3QaXI1xXL9XPZYt+IKino/rUve1vwh/GFc8xrJlLCzXl3C53MpJFQpTvoKUsd UT184n2cTyc4ai9ZVIsaABOn2yzeuWpeQtb114GY3iSyPxXaT5mNYO3zuvKYTCb7vUeD j1kvRiYpQCTw4RwdblWIk+ZLCCr6yAcYsxg9ZO8+t5UZiwE4eOQL/APxfodr7U74LtfK Mh0g== X-Gm-Message-State: AOJu0YxAdNlzqBKzyaRRcMMFhloLf9a+8EAmXMPmxFlety9hD6zfyvCj 5f67EvCgcQ+JUnzgSIpHjON2O4OeWJZ5S36xAQ1IkLQS1yBOEGOT9S540r/35CBioV3lME6O49V 5J2pVKrJ0roy876sjacipPigw/yqe1nM= X-Gm-Gg: ASbGncuQMawKQMv8dYJXn+d7T/5gjJiv4PzXGWzdZdPbAoGl8riuRFJhCE3csuFAqdn xjX3ISaoVM4YcjtOqmgp0lx2jnXyAkI9POwZDctWNnbtWl0BzzscZIHXmIwWVwsaJWnwGHzQ1dA 55hnUPHOUquYE28pbpZayE10pHLLvgWLfkGN0dFVtVPaI7l1//YzQhzBDLEiEweOQ7ZBBjYu0Tg lSlm4vXRQHtfrBap+FIEYTWJj6N49ba62F2R2+9yLL0EhvovLNkIHm3JQfvrc9rKUBfJA== X-Google-Smtp-Source: AGHT+IHNRlgUc7zAukl9qKx+JdkYQbfASjjHzHrBpt5wmeWjG+b6hSxRerNpomld9Hp6RzM0BVglLAXKSgDp26Sg2p0= X-Received: by 2002:a05:6820:4dc9:b0:657:5723:76ce with SMTP id 006d021491bc7-6597bbc8b1dmr1867625eaf.5.1764878616375; Thu, 04 Dec 2025 12:03:36 -0800 (PST) MIME-Version: 1.0 Received: by 2002:a05:6802:1d83:10b0:605:9682:fa2a with HTTP; Thu, 4 Dec 2025 12:03:35 -0800 (PST) In-Reply-To: References: From: "David G. Johnston" Date: Thu, 4 Dec 2025 13:03:35 -0700 X-Gm-Features: AWmQ_bmA0FnLcm6J6Le5u5TLiA3hWLIfjpe8iH7SCj8749c6zbrajG6D0uwlcQc Message-ID: Subject: Re: Extract only maximum date from column To: Rich Shepard Cc: "pgsql-general@postgresql.org" Content-Type: multipart/alternative; boundary="0000000000000033f7064525d361" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000000033f7064525d361 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thursday, December 4, 2025, Rich Shepard wrote: > I want the script to extract only the maximum `next_contact' date and > haven't learned how to do this from my reading of web sites. > > The script: > > select p.person_nbr, p.company_nbr, c.next_contact > from people as p, contacts as c > where c.next_contact >=3D '2025-11-01' > group by p.person_nbr, p.company_nbr, c.next_contact > order by p.person_nbr, p.company_nbr, max(c.next_contact); > > returns all contacts rather than only the latest one. > > Is using a sub-select the proper way? > 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. You also seem to have completely missed joining people to contracts - suggest using explicit =E2=80=9Cjoin=E2=80=9D clauses to make that error le= ss likely. David J. --0000000000000033f7064525d361 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thursday, December 4, 2025, Rich Shepard <rshepard@appl-ecosys.com> wrote:
I want the script to extract only the maximum `next_contact= ' date and
haven't learned how to do this from my reading of web sites.

The script:

select p.person_nbr, p.company_nbr, c.next_contact
from people as p, contacts as c
where c.next_contact >=3D '2025-11-01'
group by p.person_nbr, p.company_nbr, c.next_contact
order by p.person_nbr, p.company_nbr, max(c.next_contact);

returns all contacts rather than only the latest one.

Is using a sub-select the proper way?

I would go with a lateral join subquery of= the contracts table.=C2=A0 Using an aggregates to perform ranking is an an= ti-pattern. You want the contract ranked first when ordered by contract_dat= e.=C2=A0 Either use a window function to explicitly rank the contracts or u= se a limit/fetch clause to simply return the first ordered one.
<= br>
You also seem to have completely missed joining people to con= tracts - suggest using explicit =E2=80=9Cjoin=E2=80=9D clauses to make that= error less likely.

David J.

--0000000000000033f7064525d361--