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 252329FA5D2 for ; Thu, 1 Jun 2006 15:00:11 -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 39628-10 for ; Thu, 1 Jun 2006 15:00:05 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from he100.hostelement.com (unknown [64.246.26.51]) by postgresql.org (Postfix) with ESMTP id 001479FA040 for ; Thu, 1 Jun 2006 15:00:05 -0300 (ADT) Received: from [64.213.65.29] (helo=[127.0.0.1]) by he100.hostelement.com with esmtpa (Exim 4.52) id 1FlrSh-0005uf-VY; Thu, 01 Jun 2006 14:00:00 -0400 Message-ID: <447F2B19.1090608@oisinglynn.com> Date: Thu, 01 Jun 2006 13:59:53 -0400 From: Oisin Glynn User-Agent: Thunderbird 1.5.0.2 (Windows/20060308) MIME-Version: 1.0 To: Collin Peters CC: pgsql-sql@postgresql.org Subject: Re: Am I crazy or is this SQL not possible References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - he100.hostelement.com X-AntiAbuse: Original Domain - postgresql.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - oisinglynn.com X-Source: X-Source-Args: X-Source-Dir: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/17 X-Sequence-Number: 25253 Collin Peters wrote: > I am having some serious mental block here. Here is the abstract > version of my problem. I have a table like this: > > unique_id (PK) broadcast_id date_sent status > 1 1 2005-04-04 30 > 2 1 2005-04-01 30 > 3 1 2005-05-20 10 > 4 2 2005-05-29 30 > > So it is a table that stores broadcasts including the broadcast_id, > the date sent, and the status of the broadcast. > > 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. > > SELECT MAX(date_sent), status > FROM broadcast_history > GROUP BY broadcast_id > > How do I get the status for the most recent date_sent using GROUP BY? > > DISTINCT also doesn't work > > SELECT DISTINCT ON (email_broadcast_id) * > FROM email_broadcast_history > ORDER BY date_sent > > As you have to have the DISTINCT fields matching the ORDER BY fields. > I have to ORDER BY date_sent, but I can't do a DISTINCT on date_sent > > I keep thinking am I missing something. Does anybody have any ideas? > > ---------------------------(end of broadcast)--------------------------- > TIP 2: Don't 'kill -9' the postmaster May not be the most efficient but seems to work here. Select broadcast_id,status from broadcast_history bh1 where bh1.date_sent = (select max(date_sent) from broadcast_history bh2 where bh1.broadcast_id=bh2.broadcast_id) order by bh1.broadcast_id; Oisin