Re: sorting and grouping with min/max

From: Andreas Kretschmer <akretschmer(at)spamfence(dot)net>
To: pgsql-novice(at)postgresql(dot)org
Subject: Re: sorting and grouping with min/max
Date: 2009-03-01 13:08:49
Message-ID: 20090301130849.GA9599@tux
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Valentin Gjorgjioski <tinodj(at)mt(dot)net(dot)mk> wrote:
>> i'm looking for a query that returns one row for each player with the
>> smallest cmd_nr value. after several hours i figured out the following
>> query
>>
>> SELECT * FROM command_queue GROUP BY id, params, player, cmd_nr, date
>> HAVING (player, cmd_nr) IN (SELECT player, MIN(cmd_nr) FROM
>> command_queue GROUP BY player)
>
> well... for sure it would be simpler if you say
>
> SELECT * FROM command_queue where
> (player, cmd_nr) IN (SELECT player, MIN(cmd_nr) FROM command_queue GROUP
> BY player)
>
> But, can be even simpler? I hate subqueries...

Maybe it is faster with an JOIN instead the IN(...), other solution:
wait for 8.4 windowing functions...

Andreas
--
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect. (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly." (unknown)
Kaufbach, Saxony, Germany, Europe. N 51.05082°, E 13.56889°

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Valentin Gjorgjioski 2009-03-01 16:26:43 Re: sorting and grouping with min/max
Previous Message Valentin Gjorgjioski 2009-03-01 12:50:34 Re: sorting and grouping with min/max