Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk> writes:
> What I am trying to do is get a set of rows out of a table based on a
> set of grouping constraints. A simplified form of the table I am using
> is:-
> CREATE TABLE grouptest (
> rowid serial PRIMARY KEY,
> grouping integer,
> tstval integer,
> other character varying);
> I want to get out rows of data where tstval is at a minimum value for
> each set GROUPed by grouping. If there is more than one row with the
> same minimal value for tstval then I want any of those rows (but the
> data for a row must be consistent).
If you don't mind using a Postgres-specific construct, SELECT DISTINCT ON
works nicely for this. See the "weather reports" example in the SELECT
reference page.
regards, tom lane