From: Tom Lane <tgl@sss.pgh.pa.us>
To: Philip Warner <pjw@rhyme.com.au>
Cc: Josh Berkus <josh@agliodbs.com>
Cc: pgsql-sql@postgresql.org
Subject: Re: Re: Restriction by grouping problem.
Date: Sun, 29 Jul 2001 16:02:32 -0400
Message-ID: <2229.996436952@sss.pgh.pa.us> (raw)
In-Reply-To: <3.0.5.32.20010728113151.02c9a5c0@mail.rhyme.com.au>
References: <3.0.5.32.20010728100308.02c98800@mail.rhyme.com.au>
<3.0.5.32.20010728113151.02c9a5c0@mail.rhyme.com.au>
Philip Warner <pjw@rhyme.com.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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-sql@postgresql.org
Cc: tgl@sss.pgh.pa.us, pjw@rhyme.com.au, josh@agliodbs.com
Subject: Re: Re: Restriction by grouping problem.
In-Reply-To: <2229.996436952@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox