Re: Column as result of subtraction of two other columns?

From: John Sidney-Woollett <johnsw(at)wardbrook(dot)com>
To: Bruno Wolff III <bruno(at)wolff(dot)to>
Cc: Mark Cave-Ayland <m(dot)cave-ayland(at)webbased(dot)co(dot)uk>, pgsql-general(at)postgresql(dot)org
Subject: Re: Column as result of subtraction of two other columns?
Date: 2004-07-16 15:21:38
Message-ID: 40F7F282.4050608@wardbrook.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Try

select a, b, (b - a) as diff from (
select 1 as a, 2 as b
) as tmp;

John Sidney-Woollett

Bruno Wolff III wrote:

> On Fri, Jul 16, 2004 at 15:31:33 +0100,
> Mark Cave-Ayland <m(dot)cave-ayland(at)webbased(dot)co(dot)uk> wrote:
>
>>Hi everyone,
>>
>>I'm trying to calculate an output column which is the difference of two
>>other columns in the query output; the first column is an aggregate of
>>items in stock, while the second column is an aggregate of items which
>>have been used. The third column should should be the difference of the
>>two values so I can then output all three columns in a table.
>>
>>Unfortunately I can't get this to work at the moment :(. I've simplified
>>the query down to the following:
>>
>>dev=# select 1 as a, 2 as b, (b - a) as c;
>>ERROR: column "b" does not exist
>>dev=#
>>
>>Do I need to create some form of alias so the calculation can see the
>>other columns? I am using PostgreSQL 7.4.2 on Linux.
>
>
> You can't use column aliases in other columns; you need to repeat the
> column expressions.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
> (send "unregister YourEmailAddressHere" to majordomo(at)postgresql(dot)org)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Jean-Luc Lachance 2004-07-16 15:22:13 Re: Column as result of subtraction of two other columns?
Previous Message Paul Thomas 2004-07-16 15:19:27 Re: Column as result of subtraction of two other columns?