Re: sql group by statement

From: Jean-Luc Lachance <jllachan(at)nsd(dot)ca>
To: Albrecht Berger <berger1517(at)gmx(dot)ch>
Cc: pgsql <pgsql-sql(at)postgresql(dot)org>
Subject: Re: sql group by statement
Date: 2002-09-13 15:39:13
Message-ID: 3D8206A1.1B5B04FA@nsd.ca
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

How about:

select * from <Table> where (id, val2) in ( select id, max(val2) from
<Table> group by id);

JLL

Albrecht Berger wrote:
>
> Hello,
> I have a problem, which I'm not able to solve with a simple query :
>
> I need a resultset with distinct id's, but the max val2 of each id.
> I tried to group by id, but though I need the pk in my resultset
> I have to group it too, which "destroys" the group of val2.
>
> Can this be done without a huge query ?
>
> Table :
> pk id val1 val2
> 1 1 2 3
> 2 1 2 4
> 3 2 1 1
> 4 1 0 5
> 5 2 1 8
>
>
> Needed Result :
> pk id val1 val2
> 4 1 0 5
> 5 2 1 8
>
>
> Thx
> berger
>
> ---------------------------(end of broadcast)---------------------------
> TIP 6: Have you searched our list archives?
>
> http://archives.postgresql.org

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Andrew Perrin 2002-09-13 15:45:10 Re: LIMIT within UNION?
Previous Message Jan Wieck 2002-09-13 15:19:11 Re: [SQL] Latitude / Longitude