select min row in a group

From: Gregory Seidman <gss+pg(at)cs(dot)brown(dot)edu>
To: PostgreSQL general mailing list <pgsql-general(at)postgresql(dot)org>
Subject: select min row in a group
Date: 2002-06-28 21:56:17
Message-ID: 20020628215617.GA23790@cs.brown.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

This is really an SQL question, not specific to PostgreSQL. I have a table:

ValTable
======
id int
ext_id int
value float

For each ext_id (which references something in another table), I want to
find the row with the minimum value. The best I can come up with is:

SELECT * FROM ValTable AS v
WHERE v.value = (
SELECT DISTINCT min(value) FROM ValTable
WHERE ext_id = v.ext_id
)

I feel like there has to be a way to do it without a nested query, probably
using GROUP BY. Any thoughts?

--Greg

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Randall Barber 2002-06-28 21:58:54 Triggers and Functions
Previous Message Kevin Brannen 2002-06-28 21:51:05 Re: serial columns & loads misfeature?