Re: SQL Question

Поиск
Список
Период
Сортировка
От Julian Scarfe
Тема Re: SQL Question
Дата
Msg-id 004a01c5425b$c6b4c500$0600a8c0@Wilbur
обсуждение исходный текст
Ответ на SQL Question  (Alex <alex@meerkatsoft.com>)
Ответы Re: SQL Question  (Alex <alex@meerkatsoft.com>)
Список pgsql-general
From: "Alex" <alex@meerkatsoft.com>

> - How can i select only the newest record for each ProdId ?
>
> 100    | 2005-04-01
> 200    | 2005-04-01
>

DISTINCT ON was made for this and on the similar tables I have performs
rather more efficiently than using a subquery.

select distinct on (ProdId) ProdId , LastUpdate
from produpdate
order by ProdId , LastUpdate desc;

> - How can i select to retrieve the last 2 dates in record
>
> 100    | 2005-04-01 | 2005-03-01
> 200    | 2005-04-01 | 2005-03-01

To get the previous one, my first thought is something like:

select distinct on (ProdId) ProdId , LastUpdate
from produpdate p1
where LastUpdate <> (
    select max(LastUpdate ) from produpdate p2 where p2.ProdId = p1.ProdId
)
order by ProdId , LastUpdate desc ;

but there may be a much more efficient way of getting the nth result in
general.

Julian Scarfe



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

Предыдущее
От: Costin Manda
Дата:
Сообщение: Division by zero
Следующее
От: Poul Møller Hansen
Дата:
Сообщение: Re: Loosing connection with the database