pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Greg Stark <gsstark@mit.edu>
To: Gerardo Herzig <gherzig@fmed.uba.ar>
Cc: pgsql-sql@postgresql.org
Subject: Re: an aggregate to return max() - 1 value?
Date: Thu, 4 Mar 2010 20:53:10 +0000
Message-ID: <407d949e1003041253g1812b85bhe8ce0fbd7b1f7159@mail.gmail.com> (raw)
In-Reply-To: <4B901488.6080502@fmed.uba.ar>
References: <20100304200913.GA24865@apartia.fr>
	<4B901488.6080502@fmed.uba.ar>

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



view thread (6+ messages)  latest in thread

Message-ID: <407d949e1003041253g1812b85bhe8ce0fbd7b1f7159@mail.gmail.com>
Permalink:  ../407d949e1003041253g1812b85bhe8ce0fbd7b1f7159@mail.gmail.com/
Also on:    postgresql.org/message-id/407d949e1003041253g1812b85bhe8ce0fbd7b1f7159@mail.gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: gsstark@mit.edu, gherzig@fmed.uba.ar
  Subject: Re: an aggregate to return max() - 1 value?
  In-Reply-To: <407d949e1003041253g1812b85bhe8ce0fbd7b1f7159@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox