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

From: Rob Sargent <robjsargent(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: get first and last row in one sql as two columns
Date: 2015-09-02 23:30:13
Message-ID: 55E78685.3020701@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 09/02/2015 05:14 PM, 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
>
> I saw some posting in wiki with a custom function (or C extention)
> to do this. Is it widely used and reliable?
> https://wiki.postgresql.org/wiki/First/last_(aggregate)
> <https://wiki.postgresql.org/wiki/First/last_%28aggregate%29>
>
> I am wondering why these two functions are not part of postgresql built-in
> functions as it has many use cases
>
> Thanks
But what I think you are looking for are the windowing functions as what
you propose breaks down pretty quickly with more complicated queries.

In response to

Browse pgsql-general by date

  From Date Subject
Next Message ray madigans.org 2015-09-02 23:43:24 Retrieve postgres password
Previous Message Rob Sargent 2015-09-02 23:27:13 Re: get first and last row in one sql as two columns