Re: sql help, reusing a column - Mailing list pgsql-general

From Thomas Kellerer
Subject Re: sql help, reusing a column
Date
Msg-id hrd01k$qpu$1@dough.gmane.org
Whole thread Raw
In response to sql help, reusing a column  (Andy Colson <andy@squeakycode.net>)
Responses Re: sql help, reusing a column  (Andy Colson <andy@squeakycode.net>)
List pgsql-general
Andy Colson wrote on 29.04.2010 23:51:
> Here is my query, which works:
>
>
> select organization,
> state,
> (select max(idate) from times where customers.custid=times.custid and
> taskid = 27) as lastdate,
> age( (select max(idate) from times where customers.custid=times.custid
> and taskid = 27) )
> from customers
> order by lastdate desc nulls last;
>
>
> I'd love to use age(lastdate) instead of age( (repeat sql) ), but it
> does not seem to work.

This should work:

SELECT organization, state, lastdate, age(lastdate)
FROM (
   SELECT organization,
          state,
          (select max(idate) from times where customers.custid=times.custid and taskid = 27) as lastdate
   FROM customers
) t
order by lastdate desc


pgsql-general by date:

Previous
From: Andy Colson
Date:
Subject: Re: sql help, reusing a column
Next
From: Craig Ringer
Date:
Subject: Re: Recovering Data from a crashed database