Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id C904DB5DBE0 for ; Fri, 23 Sep 2011 01:40:29 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.29]) (amavisd-maia, port 10024) with ESMTP id 19016-07 for ; Fri, 23 Sep 2011 04:40:23 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from newserver.jfcomputer.com (unknown [76.14.161.106]) by mail.postgresql.org (Postfix) with ESMTP id 64563B5DBD1 for ; Fri, 23 Sep 2011 01:40:23 -0300 (ADT) Received: from localhost (unknown [127.0.0.1]) by newserver.jfcomputer.com (Postfix) with ESMTP id 38ACC1236B for ; Fri, 23 Sep 2011 04:40:22 +0000 (UTC) X-Virus-Scanned: amavisd-new at site Received: from newserver.jfcomputer.com ([127.0.0.1]) by localhost (linux-jfp8.site [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id z6LexJt5wfze for ; Thu, 22 Sep 2011 21:40:17 -0700 (PDT) Received: from linux-desktop.localnet (unknown [192.168.1.108]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: johnf@jfcomputer.com) by newserver.jfcomputer.com (Postfix) with ESMTPSA id 51BAF4ED6 for ; Thu, 22 Sep 2011 21:40:17 -0700 (PDT) From: John Fabiani Organization: J.F. Computer Solutions To: pgsql-sql@postgresql.org Subject: Re: using the aggregate function max() Date: Thu, 22 Sep 2011 21:40:16 -0700 User-Agent: KMail/1.13.5 (Linux/2.6.34.10-0.2-desktop; KDE/4.4.4; i686; ; ) References: <201109221949.20671.johnf@jfcomputer.com> <5EABD4DE-5CB8-4C65-A1FF-3166A683EE3F@yahoo.com> In-Reply-To: <5EABD4DE-5CB8-4C65-A1FF-3166A683EE3F@yahoo.com> MIME-Version: 1.0 Content-Type: Text/Plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit Message-Id: <201109222140.16949.johnf@jfcomputer.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.106 tagged_above=-5 required=5 tests=BAYES_00=-1.9, RDNS_NONE=0.793, TO_NO_BRKTS_NORDNS=0.001 X-Spam-Level: X-Archive-Number: 201109/67 X-Sequence-Number: 35867 On Thursday, September 22, 2011 08:14:58 pm David Johnston wrote: > On Sep 22, 2011, at 22:49, John Fabiani wrote: > > Hi, > > I need a little help understanding how to attack this problem. > > > > I need to find the max(date) of a field but I need that value later in my > > query. > > > > If I > > select max(x.date_field) as special_date from (select date_field) from > > table where ...)x > > > > I get one row and column. > > > > But now I want to use that field in the rest of the query > > > > select y.*, max(x.date_field) as special_date from (select date_field) > > from table where ...)x > > from aTable y where y.somefield = special_date. > > > > The above only returns one row and one column the "special_date." > > > > How can I use the aggregate field "special_date" in the rest of the > > query? Or is there some other way? > > > > Johnf > > Your query above is syntactically invalid. Try this. > > WITH max_date AS ( select max(datefield) AS specialdate from ...) > SELECT * > FROM table > JOIN max_date ON table.somefield = max_date.specialdate; > > You can use a online query instead of the WITH if desired, same effect. > > You could also drop the join and use the max_date CTE in a WHERE clause: > > ... FROM table WHERE table.somefield = (SELECT specialdate FROM max_date) > > David J thanks - I'll look into the "with" Johnf