Re: Simple question about SUM, but I can't figure it out

From: MalayS <malays_at_aol.com>
Date: 1996/05/09
Message-ID: <4mtuls$s6i_at_newsbf02.news.aol.com>#1/1


Reply :

Do this

   select sum(decode(col2l, 1, col, 2, col, 3, -col, 4, -col, 5, -col))

      from test;

Hope this answers ur question

Malay Shah
malays_at_silverline.com

--------------Original

message----------------------------------------------

Subject: Simple question about SUM, but I can't figure it out From: Dwayne King <dwayne_at_dwayne.FreeNet.Kiev.UA> Date: 8 May 1996 23:04:46 +0300
Message-ID: <AAn7BanKp5_at_dwayne.FreeNet.Kiev.UA>

Hi there.

I have what seems like a simple problem, but can't seem to figure out how to
solve it.

I have a table with one column containing a sum, and the other specifying if it
is a debit or a credit (+ or -). Now what I want to do is use the SUM command
to total the column, but I need some way of telling SUM to treat certain values
as debits, and certain others as credits.

Of course I can do this in two selects like below, but I would prefer one.

SQL> select sum(col2) from test where col1 in (1,2); <----means credit

SUM(COL2)



 3

SQL> select sum(-col2) from test where col1 in (3,4,5); <----means debit

SUM(-COL2)



 -12

Should I be using DECODE here? Will I have to use the two select method?

Thanks ahead of time. Please email as well as post - having trouble with the news server.

---



***
* Dwayne K. King * email: dwayne_at_dwayne.freenet.kiev.ua
 *
* International Monetary Fund * phone: (011-380-44) 212-54-81
 *
**************************************************************************
***
Received on Thu May 09 1996 - 00:00:00 CEST

Original text of this message