View previous topic :: View next topic |
Author |
Message |
gans79 Beginner
Joined: 31 Aug 2005 Posts: 51 Topics: 27
|
Posted: Thu Aug 12, 2010 9:29 am Post subject: DB2 query to Sum Negatives and Positives seperately |
|
|
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 |
|
 |
dbzTHEdinosauer Supermod
Joined: 20 Oct 2006 Posts: 1411 Topics: 26 Location: germany
|
Posted: Thu Aug 12, 2010 9:43 am Post subject: |
|
|
use a CASE statement _________________ Dick Brenholtz
American living in Varel, Germany |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Thu Aug 12, 2010 10:22 am Post subject: |
|
|
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 |
|
 |
gans79 Beginner
Joined: 31 Aug 2005 Posts: 51 Topics: 27
|
Posted: Fri Aug 13, 2010 1:05 am Post subject: |
|
|
Thanks Kolusu and everybody !! it worked , It saved me lot of time
Truly appreciate the help you & your team are providing to the MVS community .
Gans79 |
|
Back to top |
|
 |
|
|