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

From: Greg Stark <gsstark(at)mit(dot)edu>
To: Gerardo Herzig <gherzig(at)fmed(dot)uba(dot)ar>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: an aggregate to return max() - 1 value?
Date: 2010-03-04 20:53:10
Message-ID: 407d949e1003041253g1812b85bhe8ce0fbd7b1f7159@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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)

--
greg

In response to

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Pavel Stehule 2010-03-05 06:33:01 Re: an aggregate to return max() - 1 value?
Previous Message Rob Sargent 2010-03-04 20:20:23 Re: an aggregate to return max() - 1 value?