Re: retrieving primary key for row with MIN function

Поиск
Список
Период
Сортировка
От Jasen Betts
Тема Re: retrieving primary key for row with MIN function
Дата
Msg-id gtc4mb$4o1$7@reversiblemaps.ath.cx
обсуждение исходный текст
Ответ на retrieving primary key for row with MIN function  (Marcin Krol <mrkafk@gmail.com>)
Список pgsql-novice
On 2009-04-29, Marcin Krol <mrkafk@gmail.com> wrote:
> Hello everyone,
>
> I need to retrieve PK (r.id in the query) for row with
> MIN(r.start_date), but with a twist: I need to select only one record,
> the one with minimum date.

use LIMIT.

eg:

SELECT
   h.id  AS host_id,
   r.start_date AS reservation_start_date,
   r.id AS reservation_id
FROM hosts h
LEFT OUTER JOIN reservation_hosts rh ON rh.host_id = h.id
LEFT OUTER JOIN reservation r ON r.id = rh.reservation_id AND
(r.start_date, r.end_date) OVERLAPS ('2009-04-29'::date,'2010-04-29'::date)
ORDER BY reservation_start_date ASC
LIMIT 1



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

Предыдущее
От: Rodrigo E. De León Plicet
Дата:
Сообщение: Re: Funny foreign keys
Следующее
От: daq
Дата:
Сообщение: Re: retrieving primary key for row with MIN function