select min row in a group - Mailing list pgsql-general

From Gregory Seidman
Subject select min row in a group
Date
Msg-id 20020628215617.GA23790@cs.brown.edu
Whole thread Raw
Responses Re: select min row in a group  (Manfred Koizar <mkoi-pg@aon.at>)
List 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




pgsql-general by date:

Previous
From: Randall Barber
Date:
Subject: Triggers and Functions
Next
From: Gregory Seidman
Date:
Subject: Re: serial columns & loads misfeature?