pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
To: pgsql-sql@postgresql.org
Subject: Re: an aggregate to return max() - 1 value?
Date: Sat, 6 Mar 2010 14:31:27 +0100
Message-ID: <20100306133127.GA6562@apartia.fr> (raw)
In-Reply-To: <407d949e1003041253g1812b85bhe8ce0fbd7b1f7159@mail.gmail.com>
References: <20100304200913.GA24865@apartia.fr>
	<4B901488.6080502@fmed.uba.ar>
	<407d949e1003041253g1812b85bhe8ce0fbd7b1f7159@mail.gmail.com>

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,



view thread (6+ messages)

Message-ID: <20100306133127.GA6562@apartia.fr>
Permalink:  ../20100306133127.GA6562@apartia.fr/
Also on:    postgresql.org/message-id/20100306133127.GA6562@apartia.fr

 · 

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: vindex+lists-pgsql-sql@apartia.org
  Subject: Re: an aggregate to return max() - 1 value?
  In-Reply-To: <20100306133127.GA6562@apartia.fr>

* 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