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 DEFCD9FA5E1 for ; Fri, 2 Jun 2006 06:01:14 -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 65807-07 for ; Fri, 2 Jun 2006 06:01:02 -0300 (ADT) X-Greylist: delayed 00:15:24.17709 by SQLgrey- Received: from smtp3.lc.anpe.fr (smtp3.lc.anpe.fr [80.118.163.18]) by postgresql.org (Postfix) with ESMTP id 2C4819FA111 for ; Fri, 2 Jun 2006 06:01:02 -0300 (ADT) Received: from HOMERE01.anpe.fr (localhost.localdomain [127.0.0.1]) by smtp3.lc.anpe.fr (antivirus) with ESMTP id D627D73C91; Fri, 2 Jun 2006 10:45:33 +0200 (CEST) Received: from [10.0.32.170] ([10.0.32.170]) by HOMERE01.anpe.fr with Microsoft SMTPSVC(6.0.3790.1830); Fri, 2 Jun 2006 10:45:09 +0200 Message-ID: <447FFAAF.9050401@anpe.fr> Date: Fri, 02 Jun 2006 10:45:35 +0200 From: Patrick Jacquot User-Agent: Mozilla Thunderbird 1.0 (Windows/20041206) X-Accept-Language: en-us, en 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-OriginalArrivalTime: 02 Jun 2006 08:45:09.0336 (UTC) FILETIME=[DABA5D80:01C68620] X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/37 X-Sequence-Number: 25273 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 > You can also try SELECT * from broadcast_history A WHERE NOT EXISTS (SELECT * from broadcast_history B WHERE B.date_sent >A.date_sent) There isn't any PostgreSQL-ism, just a correlated subrequest wich is perfectly standars, afaik