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 1vRFWW-00AQT4-01 for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 20:01:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vRFWU-004T7S-2T for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 20:01:55 +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 1vRFWU-004T75-0u for pgsql-general@lists.postgresql.org; Thu, 04 Dec 2025 20:01:54 +0000 Received: from mail-qk1-x729.google.com ([2607:f8b0:4864:20::729]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vRFWS-0039rU-1z for pgsql-general@lists.postgresql.org; Thu, 04 Dec 2025 20:01:54 +0000 Received: by mail-qk1-x729.google.com with SMTP id af79cd13be357-8b2889a4451so15106385a.0 for ; Thu, 04 Dec 2025 12:01:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764878510; x=1765483310; darn=lists.postgresql.org; h=in-reply-to:content-language:from:references:to:subject:user-agent :mime-version:date:message-id:from:to:cc:subject:date:message-id :reply-to; bh=lADSQg0TUDuRr6yNc1gmiG/LEejnlRNEKJPQt9tCxV4=; b=QWr3Ri1+4/ohG9R5Xx1/y1AMoH3l7hSi4KObFKIjTSQbEmEU+tjcn3MElsD/m1ZtMm EeZUw76tIbg6JUa0VqzkHn2RKVF2+Ozr33oWIFH+6NyUWTBvYg/fODa5lNJSUWE916oG 9urf7vH3Qmbxys8b2DDseF2xj+ot64GAE6asVGxpA12gS0lIAsY3GZWtBmrVz4I4mBAW Dfz5I76J2IlGm8QdevBG4Zq48DaGYG4CrsjjYw541G3cNA4/xvDHt5u2/jHU/y1nextS M9phMzFQVH3TJdSGZ/xgWkGPCE5N93ZELXX3/XS4p0XWZ19bsy+lroC6C6ZQji1XQaB7 h5Ig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764878510; x=1765483310; h=in-reply-to:content-language:from:references:to:subject:user-agent :mime-version:date:message-id:x-gm-gg:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=lADSQg0TUDuRr6yNc1gmiG/LEejnlRNEKJPQt9tCxV4=; b=sDnHf+EfYzsoqv45sMVEUyxhIdo5jpZooW2HPk/GcWSMqCl68h0NOXF6oDYs5OZ5Tf VO7ya5fRHsccpQL4dENzCqCm6pQuwgspFQvzjvu0N8fWiJMO3V6wKZwct9OLdfv4+/B7 80+2VKnzu/mAD9ZAwKzXWlqqCgNUqkqQGfKFZr1nsW9wWaBhFcs0e59F81ySn+2b8qcC mLW0u19sMO6Iln8AcjJWuXA3Cvzsw9rbq5DtEIAqMRuT8fstWdQyWX+Q7KNE+bX+ymNP sUoZ/GsQ4CrhN0vHDGIHnemUp9bxxGYNxvbBgGZBjaN4BwnPws8fTwlOCQS4V0PCbI+5 29Pg== X-Gm-Message-State: AOJu0Yx8pCsa0t+aWr1KYl+tEzFCE3V2wRaAsm85crYiFlvaBQX4VHIT EqYRj5Yav/m8JqMlYPIF5tVMzRQ2Is5uRmgLHNmIrO389wztngaeX1p0tuI9+A== X-Gm-Gg: ASbGncumTNVQT77VE2sh5mV6m2uMg5otU2tIvipaNvfE6E63sJgeTKM+9Y6DRWWGuOv wGXWZF8995/WN1FIn5tIEDUX03tWXWj2LQzS/O2gkdRIpaqHO0HF6QgaqaaKSk+pj0ZW8YAm0iu /HAHiBTm6JIqyvXNba7zfq+sU605Xn2RQHUBRRGcDwkFkcAo+SFcdZfft+F9kcfJvsAXzqQreRf umqzMJbK+3da4BFXLdpXjSfvH1YNsDNEvujVbE+qyKuu75g9DIzOClI6444tAeMeN6QuwkxiS5f Oz7+EP5jP5vehPoZ9ZpjMpKz2xt84toFTPgjYq3kcQWLBzPLIalq6+UBJoWrEoeaiuUcIZnnc4J pt8ct5a4VNeE0TvsHijzHB+CMEO69j598aeKj4NJYAeBDPXP4YbhDxvBULVi65Jqt4T4KaTKuBf dnlNFeketUT2HXwiQ+qQ6cqIvXFQRo1NYf0H2jxO4CH1NR3NrGo31wbZsIf3BoEnlFuQpzqLeML RYhVw== X-Google-Smtp-Source: AGHT+IH5JMbVzj4/KvgX1svnEaLI1ZcwmLnh7bTYXNiYXrBH7drGWHkUde1v1XmGpmHnkc5uM/EOgA== X-Received: by 2002:a05:620a:6cc3:b0:8b2:9aba:e86e with SMTP id af79cd13be357-8b5e744f9b4mr705918085a.10.1764878508216; Thu, 04 Dec 2025 12:01:48 -0800 (PST) Received: from [192.168.0.115] (pool-71-191-221-203.washdc.fios.verizon.net. [71.191.221.203]) by smtp.gmail.com with ESMTPSA id af79cd13be357-8b627a9fcfdsm211340385a.51.2025.12.04.12.01.47 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 04 Dec 2025 12:01:47 -0800 (PST) Content-Type: multipart/alternative; boundary="------------9sVkhunWwu5ic09hK4akzTTG" Message-ID: Date: Thu, 4 Dec 2025 15:01:46 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Extract only maximum date from column To: pgsql-general@lists.postgresql.org References: From: Bryan Sayer Content-Language: en-US In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------9sVkhunWwu5ic09hK4akzTTG Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit I believe in general you need having c.next_contact = max(c.next_contact) (at least in ANSI SQL) Use having for after the join has occured *Bryan Sayer* Retired Demographer/Statistician /In a world in which you can be anything, *be kind*/ On 12/4/2025 2:55 PM, 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 >= '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? > > TIA, > > Rich > > --------------9sVkhunWwu5ic09hK4akzTTG Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit

I believe in general you need

having c.next_contact = max(c.next_contact)

(at least in ANSI SQL) Use having for after the join has occured

Bryan Sayer
Retired Demographer/Statistician
In a world in which you can be anything, be kind
On 12/4/2025 2:55 PM, 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 >= '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?

TIA,

Rich


--------------9sVkhunWwu5ic09hK4akzTTG--