X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (mx1.hub.org [200.46.208.251]) by postgresql.org (Postfix) with ESMTP id 892549FA5C8 for ; Thu, 1 Jun 2006 16:21:41 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.208.251]) (amavisd-new, port 10024) with ESMTP id 54894-07 for ; Thu, 1 Jun 2006 16:21:32 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by postgresql.org (Postfix) with ESMTP id C18DB9FA040 for ; Thu, 1 Jun 2006 16:21:32 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.13.6/8.13.6) with ESMTP id k51JLV0T014506; Thu, 1 Jun 2006 15:21:31 -0400 (EDT) To: "Collin Peters" cc: pgsql-sql@postgresql.org Subject: Re: Am I crazy or is this SQL not possible In-reply-to: References: Comments: In-reply-to "Collin Peters" message dated "Thu, 01 Jun 2006 10:43:14 -0700" Date: Thu, 01 Jun 2006 15:21:31 -0400 Message-ID: <14505.1149189691@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/22 X-Sequence-Number: 25258 "Collin Peters" writes: > What I would like to do is simply get the last date_sent and it's > status for every broadcast. I can't do a GROUP BY because I can't put > an aggregate on the status column. You missed the key idea about how to use DISTINCT ON. SELECT DISTINCT ON (email_broadcast_id) * FROM email_broadcast_history ORDER BY email_broadcast_id, date_sent DESC You order by the DISTINCT ON fields, then one or more additional fields to select the representative row you want within each DISTINCT ON group. regards, tom lane