Re: Re: Restriction by grouping problem.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Philip Warner <pjw(at)rhyme(dot)com(dot)au>
Cc: "Josh Berkus" <josh(at)agliodbs(dot)com>, pgsql-sql(at)postgresql(dot)org
Subject: Re: Re: Restriction by grouping problem.
Date: 2001-07-29 20:02:32
Message-ID: 2229.996436952@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

Philip Warner <pjw(at)rhyme(dot)com(dot)au> writes:
> This will work, but will not solve the larger problem of 'give me all the
> attrs of of the record with the least datetime for a given SID'. Jeff does
> not need this, but it is a common problem. Simply using min/max works for
> one attr, but fails to provide a consistent view of multiple attrs. PG has
> no elegant single-statement solution to this problem.

Yes it does:

SELECT DISTINCT ON (sid) * FROM logs ORDER BY sid, datetime;

This gives you just one output row per distinct "sid" value, and that
row will be the one with least datetime.

See the DISTINCT ON example in the SELECT reference page.

regards, tom lane

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Bruce Momjian 2001-07-30 03:38:22 Re: Who do I make _ not a wildcard?
Previous Message Mark kirkwood 2001-07-29 04:30:13 Re: performance issue with distance function