From: | Colin Wetherbee <cww(at)denterprises(dot)org> |
---|---|
To: | Seb <spluque(at)gmail(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: referencing column aliases in select list |
Date: | 2008-04-04 04:35:01 |
Message-ID: | 47F5AFF5.8030303@denterprises.org |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
Seb wrote:
> ---<---------------cut here---------------start-------------->---
> SELECT table1.col1 - table2.col1 AS diff1,
> table1.col2 + table2.col2 AS sum1,
> sum1 - diff1
> FROM table1 INNER JOIN table2 ON (table1.id = table2.id)
> ---<---------------cut here---------------end---------------->---
>
> but this fails with the error message that sum1 column is not known.
> How can one get around this? The calculations are quite involved and
> would be difficult and error-prone to repeat them anywhere they're
> needed in the select list. Thanks in advance for any pointers.
The way I usually handle this is as follows.
SELECT foo - bar AS baz FROM (
SELECT a.a + b.a AS foo, a.b + b.b AS bar FROM a JOIN b ON a.id = b.id
) AS subtable;
Although, I'm not really sure that's The Right Way to do it.
Colin
From | Date | Subject | |
---|---|---|---|
Next Message | Seb | 2008-04-04 04:48:56 | Re: referencing column aliases in select list |
Previous Message | Pavan Deolasee | 2008-04-04 04:31:31 | Re: [HACKERS] ANALYZE getting dead tuple count hopelessly wrong |