pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Rich Shepard <rshepard@appl-ecosys.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: Selecting table row with latest date
Date: Thu, 19 Aug 2021 10:41:22 -0400
Message-ID: <1704154.1629384082@sss.pgh.pa.us> (raw)
In-Reply-To: <alpine.LNX.2.20.2108190730510.15165@salmo.appl-ecosys.com>
References: <alpine.LNX.2.20.2108190730510.15165@salmo.appl-ecosys.com>

Rich Shepard <rshepard@appl-ecosys.com> writes:
> I have a table of contacts (PK is the person_nbr and contact_date) and I
> want to select only the row with the latest (most recent) contact_date. The
> Comparison Date/Time sections in the postgres 12 doc doesn't appear to have
> what I want, and when I try to use the max() aggregate function it throws an
> error.

The best way is usually like

    select * from mytable order by contact_date desc limit 1;

If you have an index on contact_date this should work very well indeed.

			regards, tom lane





view thread (23+ messages)  latest in thread

Message-ID: <1704154.1629384082@sss.pgh.pa.us>
Permalink:  ../1704154.1629384082@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1704154.1629384082@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, rshepard@appl-ecosys.com
  Subject: Re: Selecting table row with latest date
  In-Reply-To: <1704154.1629384082@sss.pgh.pa.us>
  References: <alpine.LNX.2.20.2108190730510.15165@salmo.appl-ecosys.com> <1704154.1629384082@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox