Re: For each key, find row with highest value of other field

Поиск
Список
Период
Сортировка
От Fernando Hevia
Тема Re: For each key, find row with highest value of other field
Дата
Msg-id 062a01c9258c$e65aa880$8f01010a@iptel.com.ar
обсуждение исходный текст
Ответ на For each key, find row with highest value of other field  (Raj Mathur <raju@linux-delhi.org>)
Список pgsql-sql
> Raj Mathur wrote:
> 
> I have some data of the form:
> 
> Key | Date       | Value
> A   | 2008-05-01 | foo    *
> A   | 2008-04-01 | bar
> A   | 2008-03-01 | foo    *
> B   | 2008-03-04 | baz
> B   | 2008-02-04 | bar
> C   | 2008-06-03 | foo    *
> C   | 2008-04-04 | baz
> C   | 2008-03-04 | bar
> 
> Is there any way to select only the rows marked with a (*) 
> out of these without doing a join?  I.e. I wish to find the 
> row with the highest Date for each Key and use the Value from that.
> 

This should do it:

Select value from table awhere date = (select max(b.date) from table b where b.key = a.key) q;

Regards,
Fernando.



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

Предыдущее
От: "Oliveiros Cristina"
Дата:
Сообщение: Re: For each key, find row with highest value of other field
Следующее
От: Mark Roberts
Дата:
Сообщение: Re: For each key, find row with highest value of other field