Re: get first and last row in one sql as two columns

Поиск
Список
Период
Сортировка
Искать
От
Tom Smith
Тема
Re: get first and last row in one sql as two columns
Дата
Msg-id
CAKwSVFHvvcqfGLwZjbrnUWQ5jf5H0EXQJkstqM=ZcMt_zrnd-w@mail.gmail.com
Ответ на
Список
Дерево обсуждения
get first and last row in one sql as two columns Tom Smith <tomsmith1989sk@gmail.com>
Re: get first and last row in one sql as two columns "Dickson S. Guedes" <listas@guedesoft.net>
Re: get first and last row in one sql as two columns Tom Smith <tomsmith1989sk@gmail.com>
Re: get first and last row in one sql as two columns Thomas Kellerer <spam_eater@gmx.net>
Re: get first and last row in one sql as two columns Rob Sargent <robjsargent@gmail.com>
Re: get first and last row in one sql as two columns Melvin Davidson <melvin6925@gmail.com>
Re: get first and last row in one sql as two columns Tom Smith <tomsmith1989sk@gmail.com>
Re: get first and last row in one sql as two columns Rob Sargent <robjsargent@gmail.com>
Hi:

The window function works for me (with adding  limit 1 in the end to output only one row
 needed instead of many duplicate rows).

thanks very much. 

On Thu, Sep 3, 2015 at 6:51 AM, Dickson S. Guedes <listas@guedesoft.net> wrote:
On Wed, Sep 02, 2015 at 07:14:40PM -0400, Tom Smith wrote:
> Hi:
>
> I need to get the first and last tow in one sql like below
>
> select first(col1), last(col1) from table order by col1

Have you tried the window functions [1] last_value and first_value?

You could use something like:

SELECT first_value(col1) over (order by col1),
       last_value(col1)  over (order by col1)
FROM table;

It should be as simple as that, or I miss your point.


[1] http://www.postgresql.org/docs/current/static/functions-window.html


[]s
--
Dickson S. Guedes
@guediz - http://github.com/guedes

В списке pgsql-general по дате отправления
От: Dickson S. Guedes
Дата:
От: Thomas Kellerer
Дата:
FAQ