Unix Technical Forum

Perform Arithmetic On Case Statement Decisions

This is a discussion on Perform Arithmetic On Case Statement Decisions within the Oracle Miscellaneous forums, part of the Oracle Database category; --> Hi, I have a case statement that's kind of like this: select name, case when before = 'x' then ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database > Oracle Miscellaneous

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-08-2008, 12:20 PM
jim
 
Posts: n/a
Default Perform Arithmetic On Case Statement Decisions

Hi,

I have a case statement that's kind of like this:

select name,

case when before = 'x' then 1
when before = 'y' then 2
else 3
end as before_numeric,

case when after = 'x' then 1
when after = 'y' then 2
else 3
end as after_numeric

from table


This takes two columns of alphabetic strings and yields columns of
numeric values (before and after values). I'm currently running the
query and then doing the math in Excel after the fact (after minus
before).

Is there any way to perform a calculation on the output of a case
statement *in* the select query itself? Ideally this would return
another column containing the value of after-before.

Thanks in advance.


jim

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-08-2008, 12:20 PM
Ed Prochak
 
Posts: n/a
Default Re: Perform Arithmetic On Case Statement Decisions

On Feb 19, 9:20 am, "jim" <jimmorga...@gmail.com> wrote:
> Hi,
>
> I have a case statement that's kind of like this:
>
> select name,
>
> case when before = 'x' then 1
> when before = 'y' then 2
> else 3
> end as before_numeric,
>
> case when after = 'x' then 1
> when after = 'y' then 2
> else 3
> end as after_numeric
>
> from table
>
> This takes two columns of alphabetic strings and yields columns of
> numeric values (before and after values). I'm currently running the
> query and then doing the math in Excel after the fact (after minus
> before).
>
> Is there any way to perform a calculation on the output of a case
> statement *in* the select query itself? Ideally this would return
> another column containing the value of after-before.
>
> Thanks in advance.
>
> jim


Answer: YES

Oh you want details, why didn't you say so? 8^)

well you could put the case query in an inline view since you
specified it HAD to be in the same SQL statement.

If your main query then gets too long, you could make the inline view
into a real view.

If you consider allowing PL/SQL, then you could make a function that
performs the case logic, converting characters to the appropriate
values.

HTH,
Ed

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 04-08-2008, 12:20 PM
jim
 
Posts: n/a
Default Re: Perform Arithmetic On Case Statement Decisions

Thanks, Ed. So what would this look like inline? I'm an Oracle
novice, so elementary advice is appreciated.

Thanks again.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 04-08-2008, 12:20 PM
Ed Prochak
 
Posts: n/a
Default Re: Perform Arithmetic On Case Statement Decisions

On Feb 21, 4:00 pm, "jim" <jimmorga...@gmail.com> wrote:
> Thanks, Ed. So what would this look like inline? I'm an Oracle
> novice, so elementary advice is appreciated.
>
> Thanks again.



select name, after_numeric - before_numeric
from (
select name,
case when before = 'x' then 1
when before = 'y' then 2
else 3
end as before_numeric,
case when after = 'x' then 1
when after = 'y' then 2
else 3
end as after_numeric
from table )
;

Try that. It is an in-line view.

You really should read the manuals. I'll leave the other two options
for an exercise. They really aren't much harder that the above.
(HINTS: look up CREATE VIEW and CREATE FUNCTION for the second anfd
third option respectively)

Have a good read.
Ed

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:55 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com