return MAX and when it happened

From: Scara Maccai <m_lists(at)yahoo(dot)it>
To: pgsql-general(at)postgresql(dot)org
Subject: return MAX and when it happened
Date: 2008-11-19 14:47:57
Message-ID: 4924271D.1080407@yahoo.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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?

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Gustavo Rosso 2008-11-19 14:52:15 Encoding
Previous Message Sam Mason 2008-11-19 14:38:20 Re: Any risks in using FUNCTIONs (stored procedures) instead of raw sql queries?