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!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to use a calculated column in the same query

551174Jun 5 2009 — edited Jun 5 2009
Hi All,

I need some help with using a calculated column in the same query.
For eq

I am joining a couple of tables and some of the select columns are calculated based on the columns of the tables and i want a new column in the same query to use this calculated feild in some other calcualtion.

something like this...

select (12+3) as Sum1, (12-3) as Sum2, (Sum1 + Sum2 ) as Sum3
from dual
or
select (12+3) as "Sum1", (12-3) as "Sum2", CASE WHEN ( "Sum1" / "Sum2" * 100 > 0 ) THEN 'Yes' ELSE 'No' END
from dual


Thanks

Comments

Peter Gjelstrup
Hi,

You need to re-select it
select sum1, sum2, sum1 + sum2 sum3
  from (
           select (12+3) as Sum1, (12-3) as Sum2 from dual
);
Regards
Peter
SanjayRs
Something like this
with data as ( 
select (12+3) as "Sum1", 
  (12-3) as "Sum2" from dual 
)
--
select "Sum1", "Sum2",  
  CASE WHEN ( "Sum1" / "Sum2" * 100 > 0 ) 
  THEN 'Yes' ELSE 'No' END
from data
/


      Sum1       Sum2 CAS
---------- ---------- ---
        15          9 Yes
SS
unknown-698157
select sum1, sum2, sum1+sum2 sum3
from
(select (12+3) as Sum1, (12-3) as Sum2
from dual
)

Seems to be homework time tonight.


-----------------
Sybrand Bakker
Senior Oracle DBA

Experts: those who did read documentation.
damorgan
Sure looks like a student to me. And one without an installed copy of the product.

Perhaps you should suggest a WITH CLAUSE query just to clearly show the instructor that the student was cheating.

WITH q AS (select (12+3) as Sum1, (12-3) as Sum2 from dual)
....;
Centinul
user548171 wrote:
select (12+3) as Sum1, (12-3) as Sum2, (Sum1 + Sum2 ) as Sum3
from dual
or
select (12+3) as "Sum1", (12-3) as "Sum2", CASE WHEN ( "Sum1" / "Sum2" * 100 > 0 ) THEN 'Yes' ELSE 'No' END
from dual


Thanks
What about just repeating the column values:
select (12+3) as "Sum1", (12-3) as "Sum2", CASE WHEN ( (12+3) / (12-3)  * 100  > 0 )  THEN 'Yes' ELSE 'No'  END FROM DUAL
551174
thanks for the reply
1 - 6
Locked Post
New comments cannot be posted to this locked post.

Post Details

Locked on Jul 3 2009
Added on Jun 5 2009
6 comments
1,268 views