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

From: Saad Ahmad <sahmad_at_mfa.com>
Date: 1996/05/10
Message-ID: <4mui2u$geh_at_homer.alpha.net>#1/1


Try something like:

select sum ( decode(col1,1,col2,2,col2,-col2 ) ) from test;

Dwayne King (dwayne_at_dwayne.FreeNet.Kiev.UA) wrote:
> Hi there.
> 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
> 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 *
> *****************************************************************************

--
**************************************************************
*                          Saad Ahmad                        *
*                          Senior Software Engineer          *
*                          McHugh Freeman & Associates, Inc. *
*                          Ph:  (414) 798-7457               *
*                          Fax: (414) 798-8619               *
*                          E-Mail: sahmad_at_mfa.com            *
**************************************************************
Received on Fri May 10 1996 - 00:00:00 CEST

Original text of this message