Re: How to handle a group query

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Nigel Metheringham <Nigel(dot)Metheringham(at)dev(dot)intechnology(dot)co(dot)uk>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: How to handle a group query
Date: 2004-11-04 19:15:19
Message-ID: 27973.1099595719@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Nigel Metheringham <Nigel(dot)Metheringham(at)dev(dot)intechnology(dot)co(dot)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

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message John-Paul Delaney 2004-11-04 20:49:25 Re: pg_ [dump & restore] invalid archive problem
Previous Message Nigel Metheringham 2004-11-04 17:34:03 How to handle a group query