Re: Selecting table row with latest date

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Rich Shepard <rshepard(at)appl-ecosys(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Selecting table row with latest date
Date: 2021-08-19 14:41:22
Message-ID: 1704154.1629384082@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Rich Shepard <rshepard(at)appl-ecosys(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Adrian Klaver 2021-08-19 14:42:57 Re: Selecting table row with latest date
Previous Message Rich Shepard 2021-08-19 14:41:06 Re: Selecting table row with latest date