Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id C79E1B5DC01 for ; Fri, 23 Sep 2011 03:36:45 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 64209-10 for ; Fri, 23 Sep 2011 06:36:39 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from lo.gmane.org (lo.gmane.org [80.91.229.12]) by mail.postgresql.org (Postfix) with ESMTP id 9D562B5DC2A for ; Fri, 23 Sep 2011 03:36:39 -0300 (ADT) Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1R6zNF-000867-T8 for pgsql-sql@postgresql.org; Fri, 23 Sep 2011 08:36:37 +0200 Received: from 217.110.94.67 ([217.110.94.67]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 23 Sep 2011 08:36:37 +0200 Received: from spam_eater by 217.110.94.67 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 23 Sep 2011 08:36:37 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: pgsql-sql@postgresql.org From: Thomas Kellerer Subject: Re: using the aggregate function max() Date: Fri, 23 Sep 2011 08:37:05 +0200 Lines: 29 Message-ID: References: <201109221949.20671.johnf@jfcomputer.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 217.110.94.67 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.23) Gecko/20090812 Thunderbird/2.0.0.23 Mnenhy/0.7.6.666 In-Reply-To: <201109221949.20671.johnf@jfcomputer.com> X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.229 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, RCVD_NUMERIC_HELO=1.164, RP_MATCHES_RCVD=-0.504, T_TO_NO_BRKTS_FREEMAIL=0.01 X-Spam-Level: X-Archive-Number: 201109/69 X-Sequence-Number: 35869 John Fabiani, 23.09.2011 04:49: > 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." > Not sure I undersand you correctly, but shouldn't the following be doing what you want: SELECT y.* FROM table y WHERE y.somefield = (SELECT max(x.date_field) FROM table x WHERE ...) Thomas