Re: How to handle a group query - Mailing list pgsql-novice

From Tom Lane
Subject Re: How to handle a group query
Date
Msg-id 27973.1099595719@sss.pgh.pa.us
Whole thread Raw
In response to How to handle a group query  (Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk>)
Responses Re: How to handle a group query  (Nigel Metheringham <Nigel.Metheringham@dev.intechnology.co.uk>)
List pgsql-novice
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

pgsql-novice by date:

Previous
From: Nigel Metheringham
Date:
Subject: How to handle a group query
Next
From: "John-Paul Delaney"
Date:
Subject: Re: pg_ [dump & restore] invalid archive problem