Re: return MAX and when it happened

From: Scara Maccai <m_lists(at)yahoo(dot)it>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: return MAX and when it happened
Date: 2008-11-19 15:35:34
Message-ID: 49243246.5060006@yahoo.it
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Sam Mason wrote:
> Why not just do:
>
> SELECT * FROM mytab
> ORDER BY num, mydate
> LIMIT 1;
>
> If you're trying to do more complicated things, DISTINCT ON may be more
> useful.
>

Well you guys are right; the problem is that the query is actually more
complex, I tried to simplify it for the question, but ended up with a
trivial question...
Let's see if this example is better:

CREATE TABLE mytab
(
num integer,
num1 integer,
num2 integer,
mydate timestamp
);

select MAX(num), SUM(num1)+SUM(num2), date_trunc('day', mydate) from
mytab group by date_trunc('day', mydate)

but I also want the timestamp where MAX(num) happened for each day.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Scott Marlowe 2008-11-19 15:37:08 Re: PostgreSQL 8.4 download?
Previous Message Sam Mason 2008-11-19 15:21:23 Re: return MAX and when it happened