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!

a better way to differentiate positive vs. negative numbers and sum them ?

672680Dec 14 2010 — edited Dec 14 2010
Hi, I wonder if there is a better or easier way to differentiate the positive numbers from negative ones and sum them respectively?

I come up with below:
create table t1 (num number, id varchar2(3));
insert into t1 values  (-100, 1);
insert into t1 values (50, 2);
insert into t1 values  (-10, 3);
insert into t1 values  (-20, 4);
insert into t1 values  (50, 5);
insert into t1 values  (60, 6);

select sum(decode(sign(num), 1, num, 0)) plus, sum(decode (sign(num), -1, num, 0)) minu from t1;

PLUS   MINU
160	-130
Any suggestion would be appreciated! Thanks
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 11 2011
Added on Dec 14 2010
6 comments
663 views