MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

DB2 query to Sum Negatives and Positives seperately

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
gans79
Beginner


Joined: 31 Aug 2005
Posts: 51
Topics: 27

PostPosted: Thu Aug 12, 2010 9:29 am    Post subject: DB2 query to Sum Negatives and Positives seperately Reply with quote

Hi,
I have a table A , which has a field Gain-Loss which can be either negative(Loss) or positive(Gain), How can i get the sum of Gains and Sum of Loss from a DB2 Query ?

Thanks
Gans
Back to top
View user's profile Send private message
dbzTHEdinosauer
Supermod


Joined: 20 Oct 2006
Posts: 1411
Topics: 26
Location: germany

PostPosted: Thu Aug 12, 2010 9:43 am    Post subject: Reply with quote

use a CASE statement
_________________
Dick Brenholtz
American living in Varel, Germany
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12378
Topics: 75
Location: San Jose

PostPosted: Thu Aug 12, 2010 10:22 am    Post subject: Reply with quote

gans79,

Try this untested Sql.


Code:

SELECT A.IND                                       
      ,SUM(A.LG) AS LOSS_GAIN_TOTAL               
  FROM (SELECT CASE WHEN GAIN_LOSS_COLUMN < 0             
                    THEN CHAR('LOSS')             
                    ELSE CHAR('GAIN')
                     END          AS IND 
              ,GAIN_LOSS_COLUMN   AS LG   
          FROM TABLE) A                       
 GROUP BY A.IND                                   
  ;                   


Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
gans79
Beginner


Joined: 31 Aug 2005
Posts: 51
Topics: 27

PostPosted: Fri Aug 13, 2010 1:05 am    Post subject: Reply with quote

Thanks Kolusu and everybody !! it worked , It saved me lot of time Smile
Truly appreciate the help you & your team are providing to the MVS community .

Gans79
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group