Re: an aggregate to return max() - 1 value?

From: Louis-David Mitterrand <vindex+lists-pgsql-sql(at)apartia(dot)org>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: an aggregate to return max() - 1 value?
Date: 2010-03-06 13:31:27
Message-ID: 20100306133127.GA6562@apartia.fr
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

On Thu, Mar 04, 2010 at 08:53:10PM +0000, Greg Stark wrote:
> SELECT col FROM tab ORDER BY col DESC OFFSET 1 LIMIT 1
>
> In 8.4 OLAP window functions provide more standard and flexibility
> method but in this case it wouldn't perform as well:
>
> postgres=# select i from (select i, rank() over (order by i desc) as r
> from i) as x where r = 2;
> i
> ----
> 99
> (1 row)
>
> postgres=# select i from (select i, dense_rank() over (order by i
> desc) as r from i) as x where r = 2;
> i
> ----
> 99
> (1 row)

Wow, I didn't know about window functions until now. It's exactly what I
need.

Thanks Greg, and also thanks to others who sent their suggestion.

Cheers,

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Greg Stark 2010-03-06 14:45:38 Re: Does IMMUTABLE property propagate?
Previous Message Little, Douglas 2010-03-06 13:21:57 Re: Check type compatibility