Received: from sss.pgh.pa.us ([192.204.191.242]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f6TK36f37108 for ; Sun, 29 Jul 2001 16:03:06 -0400 (EDT) (envelope-from tgl@sss.pgh.pa.us) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.11.4/8.11.4) with ESMTP id f6TK2XC02232; Sun, 29 Jul 2001 16:02:34 -0400 (EDT) To: Philip Warner cc: "Josh Berkus" , pgsql-sql@postgresql.org Subject: Re: Re: Restriction by grouping problem. 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> Comments: In-reply-to Philip Warner message dated "Sat, 28 Jul 2001 11:31:51 +1000" Date: Sun, 29 Jul 2001 16:02:32 -0400 Message-ID: <2229.996436952@sss.pgh.pa.us> From: Tom Lane X-Archive-Number: 200107/375 X-Sequence-Number: 4189 Philip Warner 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