Re: ids from grouped rows - Mailing list pgsql-sql

From Weinzierl Stefan
Subject Re: ids from grouped rows
Date
Msg-id kh22r2-upc.ln1@router.privat
Whole thread Raw
In response to ids from grouped rows  (Lindsay <hellolindsay@gmail.com>)
List pgsql-sql
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


pgsql-sql by date:

Previous
From: "Ricky Sutanto"
Date:
Subject: Counting Row
Next
From: Alexander Schreiber
Date:
Subject: Re: difference between all RDBMSs