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 4203E9FA5C8 for ; Thu, 1 Jun 2006 17:27:45 -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 62979-04 for ; Thu, 1 Jun 2006 17:27:37 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from twiddle.look.ca (beta1.look.ca [207.136.80.125]) by postgresql.org (Postfix) with ESMTP id A654C9FA040 for ; Thu, 1 Jun 2006 17:27:37 -0300 (ADT) Received: from 216-154-16-41.dsl.look.ca ([216.154.16.41]) by twiddle.look.ca with esmtp (Exim 4.20) id 1FltlW-0007ye-Qe; Thu, 01 Jun 2006 20:27:34 +0000 From: Rod Taylor To: Yasir Malik Cc: PostgreSQL In-Reply-To: References: Date: Thu, 01 Jun 2006 16:26:53 -0400 Message-Id: <1149193613.6071.18.camel@home> Mime-Version: 1.0 X-Mailer: Evolution 2.6.1 FreeBSD GNOME Team Port X-SA-Exim-Mail-From: pg@rbt.ca Subject: Re: Am I crazy or is this SQL not possible Content-Type: text/plain Content-Transfer-Encoding: 7bit X-SA-Exim-Version: 3.1 (built Tue Feb 24 05:09:27 GMT 2004) X-SA-Exim-Scanned: Yes X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200606/29 X-Sequence-Number: 25265 On Thu, 2006-06-01 at 14:13 -0400, Yasir Malik wrote: > > 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 > > > You could try the following: > select status > from broadcast_history bh > where bh.date_sent = > (select max(bh2.date_sent) > from broadcast_history bh2); > > This reminds me of an interview question: I was asked how to get a > maximum column from a table without using max. How would you do that? Find the list of everything that isn't the highest value, then invert it. Don't expect it to perform very well though. select col from foo where col not in (select f1.col from foo as f1 join foo as f2 on (f1.col < f2.col) ); --