pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
an aggregate to return max() - 1 value?
6+ messages / 5 participants
[nested] [flat]

* an aggregate to return max() - 1 value?
@ 2010-03-04 20:09 Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
  2010-03-04 20:14 ` Re: an aggregate to return max() - 1 value? Gerardo Herzig <gherzig@fmed.uba.ar>
  2010-03-04 20:20 ` Re: an aggregate to return max() - 1 value? Rob Sargent <robjsargent@gmail.com>
  2010-03-05 06:33 ` Re: an aggregate to return max() - 1 value? Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 3 replies; 6+ messages in thread

From: Louis-David Mitterrand @ 2010-03-04 20:09 UTC (permalink / raw)
  To: pgsql-sql

Hi,

With builtin aggregates is it possible to return the value just before
max(col)?

Thanks,



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: an aggregate to return max() - 1 value?
  2010-03-04 20:09 an aggregate to return max() - 1 value? Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
@ 2010-03-04 20:14 ` Gerardo Herzig <gherzig@fmed.uba.ar>
  2010-03-04 20:53   ` Re: an aggregate to return max() - 1 value? Greg Stark <gsstark@mit.edu>
  2 siblings, 1 reply; 6+ messages in thread

From: Gerardo Herzig @ 2010-03-04 20:14 UTC (permalink / raw)
  To: pgsql-sql

Louis-David Mitterrand wrote:
> Hi,
> 
> With builtin aggregates is it possible to return the value just before
> max(col)?
> 
> Thanks,
> 
Mmmm what about
select max(col) from table where col not in (select max(col) from table;
? Looks like a double table reading, but it works.

Gerardo



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: an aggregate to return max() - 1 value?
  2010-03-04 20:09 an aggregate to return max() - 1 value? Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
  2010-03-04 20:14 ` Re: an aggregate to return max() - 1 value? Gerardo Herzig <gherzig@fmed.uba.ar>
@ 2010-03-04 20:53   ` Greg Stark <gsstark@mit.edu>
  2010-03-06 13:31     ` Re: an aggregate to return max() - 1 value? Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
  0 siblings, 1 reply; 6+ messages in thread

From: Greg Stark @ 2010-03-04 20:53 UTC (permalink / raw)
  To: Gerardo Herzig <gherzig@fmed.uba.ar>; +Cc: 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



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: an aggregate to return max() - 1 value?
  2010-03-04 20:09 an aggregate to return max() - 1 value? Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
  2010-03-04 20:14 ` Re: an aggregate to return max() - 1 value? Gerardo Herzig <gherzig@fmed.uba.ar>
  2010-03-04 20:53   ` Re: an aggregate to return max() - 1 value? Greg Stark <gsstark@mit.edu>
@ 2010-03-06 13:31     ` Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
  0 siblings, 0 replies; 6+ messages in thread

From: Louis-David Mitterrand @ 2010-03-06 13:31 UTC (permalink / raw)
  To: 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,



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: an aggregate to return max() - 1 value?
  2010-03-04 20:09 an aggregate to return max() - 1 value? Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
@ 2010-03-04 20:20 ` Rob Sargent <robjsargent@gmail.com>
  2 siblings, 0 replies; 6+ messages in thread

From: Rob Sargent @ 2010-03-04 20:20 UTC (permalink / raw)
  To: pgsql-sql

'select max(col) where col < max(col)' should work but you have to do 
'where col < (select max(col) '

On 03/04/2010 01:09 PM, Louis-David Mitterrand wrote:
> Hi,
>
> With builtin aggregates is it possible to return the value just before
> max(col)?
>
> Thanks,
>



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: an aggregate to return max() - 1 value?
  2010-03-04 20:09 an aggregate to return max() - 1 value? Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
@ 2010-03-05 06:33 ` Pavel Stehule <pavel.stehule@gmail.com>
  2 siblings, 0 replies; 6+ messages in thread

From: Pavel Stehule @ 2010-03-05 06:33 UTC (permalink / raw)
  To: pgsql-sql

Hello

select min(x) from (select x from data order by x desc limit 2) s;

Pavel

2010/3/4 Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>:
> Hi,
>
> With builtin aggregates is it possible to return the value just before
> max(col)?
>
> Thanks,
>
> --
> Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-sql
>



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2010-03-06 13:31 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2010-03-04 20:09 an aggregate to return max() - 1 value? Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
2010-03-04 20:14 ` Gerardo Herzig <gherzig@fmed.uba.ar>
2010-03-04 20:53   ` Greg Stark <gsstark@mit.edu>
2010-03-06 13:31     ` Louis-David Mitterrand <vindex+lists-pgsql-sql@apartia.org>
2010-03-04 20:20 ` Rob Sargent <robjsargent@gmail.com>
2010-03-05 06:33 ` Pavel Stehule <pavel.stehule@gmail.com>

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