Re: using the aggregate function max()

From: Thomas Kellerer <spam_eater(at)gmx(dot)net>
To: pgsql-sql(at)postgresql(dot)org
Subject: Re: using the aggregate function max()
Date: 2011-09-23 06:37:05
Message-ID: j5h9da$f1q$1@dough.gmane.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Filip Rembiałkowski 2011-09-23 16:14:14 Re: help with xpath namespace
Previous Message Amar Dhole 2011-09-23 06:32:13 use of savepoint in containter managed transaction