Re: Selecting table row with latest date

Поиск
Список
Период
Сортировка
От Tom Lane
Тема Re: Selecting table row with latest date
Дата
Msg-id 1704154.1629384082@sss.pgh.pa.us
обсуждение исходный текст
Ответ на Selecting table row with latest date  (Rich Shepard <rshepard@appl-ecosys.com>)
Ответы Re: Selecting table row with latest date  (Rich Shepard <rshepard@appl-ecosys.com>)
Re: Selecting table row with latest date  (Rich Shepard <rshepard@appl-ecosys.com>)
Список pgsql-general
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



В списке pgsql-general по дате отправления:

Предыдущее
От: Rich Shepard
Дата:
Сообщение: Re: Selecting table row with latest date
Следующее
От: Adrian Klaver
Дата:
Сообщение: Re: Selecting table row with latest date