pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Weinzierl Stefan <Stefan@Weinzierl-Stefan.de>
To: pgsql-sql@postgresql.org
Subject: Re: ids from grouped rows
Date: Wed, 20 Jul 2005 16:09:23 +0200
Message-ID: <kh22r2-upc.ln1@router.privat> (raw)
In-Reply-To: <e1f838770507200546208a47fc@mail.gmail.com>
References: <e1f838770507200546208a47fc@mail.gmail.com>

Lindsay schrieb:
> Lets say i do this:
> 
> SELECT name, MAX(age)
> FROM Person
> GROUP BY name
> 
> This will group people who have the same name together, and return the
> highest age for each group. I want to be able to find the id for the
> person with the highest age in each group  -
> 
> Ideally, it would be something like this 
> 
> SELECT name, MAX(age), id_for_row_with_max_age
> FROM Person
> GROUP BY name

SELECT name, age, id FROM Person RIGHT JOIN (SELECT name, MAX(age) AS m
FROM  Person GROUP BY name) AS t ON (Person.name=t.name AND Person.age=t.m)

Stefan



view thread (3+ messages)  latest in thread

Message-ID: <kh22r2-upc.ln1@router.privat>
Permalink:  ../kh22r2-upc.ln1@router.privat/
Also on:    postgresql.org/message-id/kh22r2-upc.ln1@router.privat

 · 

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: Stefan@Weinzierl-Stefan.de
  Subject: Re: ids from grouped rows
  In-Reply-To: <kh22r2-upc.ln1@router.privat>

* 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