Re: return MAX and when it happened

From: Csaba Nagy <nagy(at)ecircle-ag(dot)com>
To: Scara Maccai <m_lists(at)yahoo(dot)it>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: return MAX and when it happened
Date: 2008-11-19 15:21:05
Message-ID: 1227108065.13522.15.camel@PCD12478
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi Scara,

This should work just fine:

select num, min(mydate) from mytab group by num order by num desc limit
1;

If you have an index on 'num' it will also be fast.

Cheers,
Csaba.

On Wed, 2008-11-19 at 08:47 -0600, Scara Maccai wrote:
> Hi all,
>
> suppose I have a table like:
>
> CREATE TABLE mytab
> (
> num integer,
> mydate timestamp
> );
>
> and I want to find MAX(num) and the "mydate" where it first happened.
>
> I guess I could use
>
> select * from mytab where num = (select MAX(num) from mytab) order by
> mydate limit 1;
>
> but that would scan the data twice (I guess...)
>
> Do I have to write my own MAX function, something like:
>
> select MYMAX(num, timestamp) from mytab
>
> which would return a custom type?
> Or is there a better way?
>
>
>
>
>
>

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Sam Mason 2008-11-19 15:21:23 Re: return MAX and when it happened
Previous Message Devrim GÜNDÜZ 2008-11-19 15:19:46 Re: PostgreSQL 8.4 download?