pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jean-Luc Lachance <jllachan@nsd.ca>
To: Albrecht Berger <berger1517@gmx.ch>
Cc: pgsql <pgsql-sql@postgresql.org>
Subject: Re: sql group by statement
Date: Fri, 13 Sep 2002 11:39:13 -0400
Message-ID: <3D8206A1.1B5B04FA@nsd.ca> (raw)
References: <002f01c25b10$6db4b940$a07ba8c0@wxp>

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



view thread (6+ messages)  latest in thread

Message-ID: <3D8206A1.1B5B04FA@nsd.ca>
Permalink:  ../3D8206A1.1B5B04FA@nsd.ca/
Also on:    postgresql.org/message-id/3D8206A1.1B5B04FA@nsd.ca

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jllachan@nsd.ca, berger1517@gmx.ch
  Subject: Re: sql group by statement
  In-Reply-To: <3D8206A1.1B5B04FA@nsd.ca>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox