Select every first/last record of a partition?

Поиск
Список
Период
Сортировка
От Andreas
Тема Select every first/last record of a partition?
Дата
Msg-id 4FBA67A7.4010302@gmx.net
обсуждение исходный текст
Ответы Re: Select every first/last record of a partition?  (Seth Gordon <sethg@ropine.com>)
Список pgsql-sql
Hi,

suppose a table that has records with some ID and a timestamp.

id,    ts
3,    2012/01/03
5,    2012/01/05
7,    2012/01/07
3,    2012/02/03
3,    2012/01/05
5,    2012/03/01
7,    2012/04/04

to fetch every last row of those IDs I do:

select   id, ts
from   (   select   id, ts, row_number() over ( partition by id   order 
by ts desc ) as nr from mytab ) as x
where nr = 1


Is there a another way without a subselect?
There might be more columns so the window-functions first/last won't help.





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

Предыдущее
От: Mario Dankoor
Дата:
Сообщение: Re: master/detail
Следующее
От: Seth Gordon
Дата:
Сообщение: Re: Select every first/last record of a partition?