Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reuse column calculations

Flamingo123-OracleJul 6 2009 — edited Jul 6 2009
In my query I have a lot of columns that are a result of complicated calculations, some of the columns can be computed as a result of other columns.
My question is without creating a nested SQL can I refer to column alias so that I don't have to specify the same calculation twice.

Below is a simplified example of what I am trying to do where the column alias num2 and num21 are simplified examples of the calculations
I need to perform. The first query below works fine but I have to specify my calculation (num *2) twice.

select num * 2 num2, (num *2) + 1 num21 from
(select 2 num from dual)

I would like to somehow refer the num2 alias in my second column as shown below. (so that (num * 2) is speicified only once) But I get a SQL error saying the num2 idenitifer is not valid.

select num * 2 num2, num2 + 1 from
(select 2 num from dual)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 3 2009
Added on Jul 6 2009
19 comments
6,654 views