Received: from maia.hub.org (maia-5.hub.org [200.46.204.29]) by mail.postgresql.org (Postfix) with ESMTP id 87CB4B5DC0F for ; Fri, 23 Sep 2011 00:15:06 -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 74220-03-2 for ; Fri, 23 Sep 2011 03:14:59 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.8.0-rc2 Received: from nm21-vm0.bullet.mail.ne1.yahoo.com (nm21-vm0.bullet.mail.ne1.yahoo.com [98.138.90.94]) by mail.postgresql.org (Postfix) with SMTP id B51C8B5DC53 for ; Fri, 23 Sep 2011 00:14:58 -0300 (ADT) Received: from [98.138.90.50] by nm21.bullet.mail.ne1.yahoo.com with NNFMP; 23 Sep 2011 03:14:56 -0000 Received: from [98.138.89.245] by tm3.bullet.mail.ne1.yahoo.com with NNFMP; 23 Sep 2011 03:14:56 -0000 Received: from [127.0.0.1] by omp1059.mail.ne1.yahoo.com with NNFMP; 23 Sep 2011 03:14:56 -0000 X-Yahoo-Newman-Id: 341726.4393.bm@omp1059.mail.ne1.yahoo.com Received: (qmail 49643 invoked from network); 23 Sep 2011 03:14:56 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.com; h=DKIM-Signature:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:Mime-Version:In-Reply-To:X-Apple-Yahoo-Original-Message-Folder:Content-Type:Content-Transfer-Encoding:Message-Id:Cc:X-Mailer:From:X-Apple-Yahoo-Replied-Msgid:Subject:Date:To; b=JBm3dRRCBMVCDhnsjjgJXtUcB7pyAKQUAZoL3BX1NGevYmyoQme6dsGfxpzXsn8KExStNCgDsJEtcNLojBvqpDPBYIVJj/sQPXFUeD4c/LD9nE74gZHKNSrWye2IBOEq/Fck3yRnDo4AV+Hp7EoBNnxxdy4actYRS1H/isCN0f0= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1316747696; bh=y8Mgnqss7wiY8R+frUn0HTQilCYZPzWAdWNEtPnwKgM=; h=X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:References:Mime-Version:In-Reply-To:X-Apple-Yahoo-Original-Message-Folder:Content-Type:Content-Transfer-Encoding:Message-Id:Cc:X-Mailer:From:X-Apple-Yahoo-Replied-Msgid:Subject:Date:To; b=zhXdPZdTi1DrwgMJdT9P1d/P6X6VM3eC/xcNhyv7KidjtOfyYiQb2i9wM4K+L81uPb3EWRqOAke20dNu/mYhD8stOGHd2M96oCpVGCqk06to05iLTZZHO0CJ/+9fTJ5XaIILS1oGsX+hPa8ksaL0HvKMfhiM8CfM5MY8I+Fk4Vc= X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: JWDWP3AVM1m5aTzrC4b2EpY.I4U6gWwfl66gZkb9fPmfjcc P2yKWPOE8kNzIbirIexeHOBHNIeuTlAelfIgFYabKlu9oNF1dtUibGktAAKq 8SJKkN4AUc4UjU92Y5Zr2qT1CS_7.cMkaaZiAa3Z_BgHjBKoWIs8rGuLJ2aS MlPFJcfvRnt3aelhnvKKQzWBfP8.53gyLja5kBq4e9yO1CZk4E_yEOjKhVzl 6vqEn42mB.G9_AfDvOmmq3L5gFI0qH4yqTAEcCEYAJ2U2dCC3pLB_hb3GJQ4 1I8N_6kOjEkfsjDydzwyDRJjxViqsrsaVHGAFhtVqngxH0odmTvnBj7ApFwj JI8obxgUkY7tO3r71DBFqOg2G7XfJBv8WnePwGreCJJ71zJqM27qPBDp2_2k KMIOsBg-- X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from [192.12.17.102] (polobo@24.93.23.188 with xymcookie) by smtp103-mob.biz.mail.ne1.yahoo.com with SMTP; 22 Sep 2011 20:14:56 -0700 PDT References: <201109221949.20671.johnf@jfcomputer.com> Mime-Version: 1.0 (iPad Mail 8J2) In-Reply-To: <201109221949.20671.johnf@jfcomputer.com> X-Apple-Yahoo-Original-Message-Folder: Inbox Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable Message-Id: <5EABD4DE-5CB8-4C65-A1FF-3166A683EE3F@yahoo.com> Cc: "pgsql-sql@postgresql.org" X-Mailer: iPad Mail (8J2) From: David Johnston X-Apple-Yahoo-Replied-Msgid: 1_335973_APKkiGIAACjvTnv2FwhnGEzNNAs Subject: Re: using the aggregate function max() Date: Thu, 22 Sep 2011 23:14:58 -0400 To: John Fabiani X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.402 tagged_above=-5 required=5 tests=BAYES_00=-1.9, FREEMAIL_FROM=0.001, MIME_QP_LONG_LINE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, RP_MATCHES_RCVD=-0.504 X-Spam-Level: X-Archive-Number: 201109/66 X-Sequence-Number: 35866 On Sep 22, 2011, at 22:49, John Fabiani wrote: > Hi, > I need a little help understanding how to attack this problem. =20 >=20 > I need to find the max(date) of a field but I need that value later in my=20= > query. >=20 > If I > select max(x.date_field) as special_date from (select date_field) from tab= le=20 > where ...)x=20 >=20 > I get one row and column. >=20 > But now I want to use that field in the rest of the query >=20 > select y.*, max(x.date_field) as special_date from (select date_field) fr= om=20 > table where ...)x =20 > from aTable y where y.somefield =3D special_date. >=20 > The above only returns one row and one column the "special_date." >=20 > How can I use the aggregate field "special_date" in the rest of the query?= Or=20 > is there some other way? >=20 > Johnf >=20 > --=20 > Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-sql 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 =3D 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 =3D (SELECT specialdate FROM max_date) David J