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 1vRSEp-00DVdJ-1q for pgsql-general@arkaria.postgresql.org; Fri, 05 Dec 2025 09:36:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vRSEn-007KFD-2k for pgsql-general@arkaria.postgresql.org; Fri, 05 Dec 2025 09:36:30 +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 1vRSEn-007KF4-1e for pgsql-general@lists.postgresql.org; Fri, 05 Dec 2025 09:36:29 +0000 Received: from mail-yw1-x1135.google.com ([2607:f8b0:4864:20::1135]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vRSEl-003Fid-0J for pgsql-general@postgresql.org; Fri, 05 Dec 2025 09:36:28 +0000 Received: by mail-yw1-x1135.google.com with SMTP id 00721157ae682-787c9f90eccso18673917b3.3 for ; Fri, 05 Dec 2025 01:36:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1764927386; x=1765532186; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=lYpL2Sx/6MOUhWkX+HVtnkseutobBCzMOkaIGlHWv6s=; b=Ftgp0MA00NaCOFcTt9MjgTPnDf2Jew+qpgRT7TQLZ0K2jKBq0VsDkl2cMK5IhuDz1N RGSjw9Ubzj+eCBJdeH9LzA0ekJnn5h1tc0x2C++vrFiUxL9pSpw5GxZeinEUTf2iEBnJ zbqZBYSWoKk/VV4owU0D6lXJjjx+ga4TKrncSOLZhi6N2p3CaAYFqbqbdIzoCEivT/41 CcI42OrO8k8OS7IYoI93nftScO7MTP1pNcIEaPqskV8Nyq0RF4zAvqCxp2fOLOd4N+Me bzangkv7Bc5PNJ0SXcTBedQsSptjJ2wDCkofwI4ukOcrSBoH+Obmc8Qi5Kp7qkZXQZhF ndlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1764927386; x=1765532186; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=lYpL2Sx/6MOUhWkX+HVtnkseutobBCzMOkaIGlHWv6s=; b=KGvuz+KwukfSJoGctHXboBU4g5jas1v2meTgOKGxd9ApFWUO1OcM34LHEaDR+cDLys ngtoWsMyQp0qndtTj1dFsYTcEbDNlMrZ3CUsFjQ4bXimNbEdtfY65pdSb75UwkUceb9a RjPumWaVi/s6vgmtz/AO8KnEh5ZPudSbx2RN3J3s0fu41cYrsEdrQzk28oUjNbKh9te7 qQXFXTzKaIAqy7ngrAmmLEXHSfreLltKShkq7L7+LFVVVH5iQ6GQUMf4s+UcifaD9b5n IgjVTDaj5HauH4wEqUDIUQvHunRydfE1b4UtoHJkwKOyV4HgUSCex0Zws6TxKYK5hJba jHEg== X-Gm-Message-State: AOJu0YyJfxjOkbH80zUk3Q6HWWaSdDJAZ6nbprEMeY5jNv/RVBOFt80c rGNtIi9eUN10g8fvy9ldg0pEkSUnGvGdQt7otkdavIX3yuNB84oII3lr7bOWfaNVCRNjgK39Mny c89vZSUF8J0VDFHDodetJPtUzlThewhzcsHXE X-Gm-Gg: ASbGncu9stggsSLu9HsuEuXPk5yDlXDybEayck6bdlt+aoCkYVpIZ24PGiNIJFKVb/5 mbLwnO6FMO8GrS2gj+TJ7oml0vzBNqf4TbvogSnouh7KCNb6WrgaWMdIQN7s/v8J8n1ZXRHaw7v rVqk5jhEjckYY3VO0joGWloHqKekHjSznTUSs853dkw5CLENW/qTZ2i6HapnTKLBoNje2Lpbs52 s8vjiIuJzUmKC6wPXER8clEBrrfxcCETraEkD14HE3SQHJglQwX0QH3vpElK8RKA8NhQTQpx0R2 CexoPH4yqt89En093yrJFV/7qO2sUWc= X-Google-Smtp-Source: AGHT+IEw0/27ol3goOeRdBrqR1sgv8nj1073m6DkzurgW1P+WYZ9GI05Rt3SV7SznPhDCdx29E6ZorekntcpW2cWtM8= X-Received: by 2002:a05:690c:6d0d:b0:786:5712:46c3 with SMTP id 00721157ae682-78c18834cb5mr46329337b3.41.1764927386378; Fri, 05 Dec 2025 01:36:26 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Bernice Southey Date: Fri, 5 Dec 2025 09:35:50 +0000 X-Gm-Features: AQt7F2qlcrM4vWglS6w_i6TkwZzFbYYTwfI6e0Ddu2kAWAFvsqbRCBs5eORuhdQ Message-ID: Subject: Re: Extract only maximum date from column To: Rich Shepard Cc: pgsql-general@postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Rich Shepard wrote: > 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. DISTINCT ON might be what you're looking for. It's an extremely useful feature in postgres and well worth understanding. Here's a nice explanation, that's similar to your case: https://www.geekytidbits.com/postgres-distinct-on/ select distinct on (p.person_nbr) p.person_nbr, p.company_nbr, c.next_contact from people as p join contacts as c using (person_nbr) where c.next_contact >= '2025-11-01' order by p.person_nbr, c.next_contact; Using the following test data: create table people(person_nbr, company_nbr) as values (1, 1), (2, 1), (3, 2); create table contacts(person_nbr, next_contact) as values (1, '2025-10-31'::date), (1, '2025-11-01'), (1, '2025, 11, 02'), (3, '2025-11-02'::date), (3, '2025-11-03'); Here's the results: person_nbr | company_nbr | next_contact ------------+-------------+-------------- 1 | 1 | 2025-11-01 3 | 2 | 2025-11-02 DISTINCT ON can use an index matching the ORDER BY. create index on contacts(person_nbr, next_contact); Add some data to make the index worth it to the planner: insert into contacts select i, '2025-11-05'::date + j from generate_series(4, 100) i, generate_series(1, 100) j; analyze contacts; Check the query plan: explain select distinct on (p.person_nbr) ... This is what you want to see: -> Index Only Scan using contacts_person_nbr_next_contact_idx on contacts c Index Cond: (next_contact >= '2025-11-01'::date) Thanks, Bernice