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 1vRIHR-00AtQ3-1T for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 22:58:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vRIHQ-005ZuA-1d for pgsql-general@arkaria.postgresql.org; Thu, 04 Dec 2025 22:58:32 +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 1vRIHQ-005Zu2-0T for pgsql-general@lists.postgresql.org; Thu, 04 Dec 2025 22:58:32 +0000 Received: from mail-ed1-x529.google.com ([2a00:1450:4864:20::529]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vRIHO-003BIg-1a for pgsql-general@postgresql.org; Thu, 04 Dec 2025 22:58:32 +0000 Received: by mail-ed1-x529.google.com with SMTP id 4fb4d7f45d1cf-6415bc0cab2so199690a12.3 for ; Thu, 04 Dec 2025 14:58:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764889109; x=1765493909; darn=postgresql.org; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=QiW6WOyNnSM/4ytrG5KBK6cx7IZwW+tqurVPw7otw0Y=; b=PFWJuFyGgYvK9GkPu1ewaB6ZVTROLy8tuy9qdQWCIIPFIKivSZgE0o9Uv4KEnudXRL 5tnwmzii+eBlH6AgGTi6+1m411P0XmZwm2J8sDzSSuToO15ZlEGYHPyv+3prw2E/ncim 0OGfcKdutYy74V7vuaXBx0F1srZEnr2oK05SQdXj3d9jfugJwn5bdnmjEDS98hiDrAXf tb5qcZWNusrDfqCHd8cIEfMLiN8CFIW3kuuPq/YdKiGUFnvAJjIHVRIoGpbpG+B9G8Di Q0iz51tw6hZc0IlOs1xgWBr+NyYSab7HCtV0pZ9sHe7PqgQtFnf8o7nkHLHSyklkjY8T v6FQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764889109; x=1765493909; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-gg:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=QiW6WOyNnSM/4ytrG5KBK6cx7IZwW+tqurVPw7otw0Y=; b=OTillS9TmE/EU2FUxhWUuZbIECr67YnV6+VL9tWS0k6HHylyVt1bZzCUr5Foxm0Ynn Cp0khY1VW+hnwkaBcru6M7J1sla/Ze/05+o8Dwnl9lfE/y6+GJ79emWj3Sly3U4NVf5e b5RC72nDXBpuwkopUmStb/zlbZKEooafqPQDOf83/47vkgzqRal6g4DS239ZNPezaEE5 N7/eV70hffRsTZBZJe3zc8J70xZ5uxQI7UL8adqgDAHeVXZ+NPvaY/7lRzZ9z3RM5Kh0 Z8TwH1n9Csfycop85JfqvLTVU7a1WtkPHWHvA5qkmhQKXTHph2l8uohJMq0SwcdtuAjp w3vg== X-Gm-Message-State: AOJu0Yy1rXREnQfZkSL3ldRb8as2WDnX6Sr3Jg78ccNHpRmozhNBmofB k9hsJaIrLuIRHUmv5L+9iLs3lckC6O6wevglDvXRZg3R55LxfvEDDeVf X-Gm-Gg: ASbGnctP8LFNqb5iRo1fLOY62oUUfevP+elAKnsXT0Rj/HrPBNXPyCFWgJ3tFWlDP8V U197KovbA63P04nwipOM46h06OL7vucPQlSzwUx0jI0SN3Ac6VSfVdWiauZGU50R8iQ3BszVU7r q1LEY6qNofYKtRwVTo618vM3zMp8OvdMW5dleBB/GPgkI6phl/vVL+r4sAr3Ki9AH/J17uKzAc0 30hc6lEnQjBwNFzdz8rD+WzKSJi6b/a8CxOK4ouYuJL1s4CAa38zkVuXr98ASsPGEtyZ2EFGp37 ylInO69YAp6JQzM/gCvYP9IS624IR2RgdZ04dqh5ht+dzkjxD5jPl7CHTTtBQfQLd0SjB4Fu6cm OyNwiZBoQQUvn6kswQlsVpaEAfcecjtjddigM9igp8Oq3Sm/A3IimoBUwkKt7UYhuhdkcfZCFP3 ES/30JsJX1NtOlESp0FsA9fCzp63gRsRAG X-Google-Smtp-Source: AGHT+IEqpNUPhQJ7zuhc2xXtCxPgOVZlw9b/qb6Iywl3y+1EKdqCVw8g6FeaO7Ib5Ko9mYq51dJYmw== X-Received: by 2002:a05:6402:26cc:b0:640:ed50:c1e4 with SMTP id 4fb4d7f45d1cf-647a0012c1dmr3792540a12.3.1764889108511; Thu, 04 Dec 2025 14:58:28 -0800 (PST) Received: from smtpclient.apple ([188.212.112.125]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-647b2edd772sm2212099a12.12.2025.12.04.14.58.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 04 Dec 2025 14:58:28 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.200.81.1.6\)) Subject: Re: Extract only maximum date from column From: Alban Hertroys In-Reply-To: Date: Thu, 4 Dec 2025 23:58:17 +0100 Cc: pgsql-general@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Rich Shepard X-Mailer: Apple Mail (2.3864.200.81.1.6) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On 4 Dec 2025, at 20:55, Rich Shepard = wrote: >=20 > 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. >=20 > The script: >=20 > 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); >=20 > returns all contacts rather than only the latest one. >=20 > Is using a sub-select the proper way? >=20 > TIA, >=20 > Rich That looks like a classical case for a correlated subquery with WHERE = NOT EXISTS. Something like: select p.person_nbr, p.company_nbr, c.next_contact from people as p join contacts as c on -- I=E2=80=99m really missing some kind of relation between p and c = here, I think that=E2=80=99s relevant where c.next_contact >=3D =E2=80=982025-11-01=E2=80=99 -- make sure there=E2=80=99s no later contact and not exists ( select 1 -- the value is irrelevant, as long as there=E2=80=99s = no later instance of a contact from contacts c2 where c2.next_contact >=3D =E2=80=982025-11-01=E2=80=99 and c2.next_contact > c.next_contact ) P.S. My mail-client tried to outsmart me in this reply (in no small = part) and I just got back from the pub, so I can=E2=80=99t exactly = guarantee correctness of the above, but the principal idea should be = solid. Alban Hertroys -- There is always an exception to always.