pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Thomas Kellerer <spam_eater@gmx.net>
To: pgsql-sql@postgresql.org
Subject: Re: using the aggregate function max()
Date: Fri, 23 Sep 2011 08:37:05 +0200
Message-ID: <j5h9da$f1q$1@dough.gmane.org> (raw)
In-Reply-To: <201109221949.20671.johnf@jfcomputer.com>
References: <201109221949.20671.johnf@jfcomputer.com>

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





view thread (4+ messages)

Message-ID: <j5h9da$f1q$1@dough.gmane.org>
Permalink:  ../j5h9da$f1q$1@dough.gmane.org/
Also on:    postgresql.org/message-id/j5h9da$f1q$1@dough.gmane.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: spam_eater@gmx.net
  Subject: Re: using the aggregate function max()
  In-Reply-To: <j5h9da$f1q$1@dough.gmane.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox