GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Oracle  >  SQL
Go To First  |  Previous Question  |  Next Question 
 SQL  |  Question 117 of 171    Print  
How to add positive and negative numbers in a single column and output them to two different columns

  
Total Answers and Comments: 4 Last Update: December 07, 2008     Asked by: srydhar 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
February 15, 2007 06:16:25   #1  
Gurwinder Singh Saini        

RE: How to add positive and negative numbers in a sing...
pn is table having +ve and -ve values SQL> select * from pn ; NUM---------- 1 2 3 -1 -4 -56 rows selected.SQL> -----Seperate positive & negative valuseSQL> SELECT (CASE WHEN num<0 THEN num ELSE 0 END) neg (CASE WHEN num>0 THEN num ELSE 0 END) pos FROM pn ; NEG POS---------- ---------- 0 1 0 2 0 3 -1 0 -4 0 -5 06 rows selected.
 
Is this answer useful? Yes | No
March 16, 2007 22:29:03   #2  
svj2000 Member Since: March 2007   Contribution: 1    

RE: How to add positive and negative numbers in a sing...

table temp with a column num of type number

select

sum(case when num > 0 then num else null end) pos

sum(case when num < 0 then num else null end) neg

from temp


 
Is this answer useful? Yes | No
December 24, 2007 08:05:20   #3  
Rina_S Member Since: December 2007   Contribution: 2    

RE: How to add positive and negative numbers in a single column and output them to two different columns

E.g. SELECT * FROM TAB1;

COL1 C
----- -
-1 A
-2 A
1 B
2 B
SELECT DECODE(SIGN(COL1) -1 col1 null) A DECODE(SIGN(COL1) 1 COL1 NULL) B FROM TAB1;

A B
----- ----------
-1
-2
1
2


 
Is this answer useful? Yes | No
December 07, 2008 01:50:53   #4  
daravipg Member Since: October 2008   Contribution: 1    

RE: How to add positive and negative numbers in a single column and output them to two different columns
select sum(decode(sign(num) 1 num 0)) pos sum(decode(sign(num) -1 num 0)) neg
from Table_name

 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape