View previous topic :: View next topic |
Author |
Message |
tattva Beginner
Joined: 02 Feb 2005 Posts: 97 Topics: 36
|
Posted: Wed Jun 08, 2005 12:27 am Post subject: Sum of 3 columns |
|
|
Hi all,
i need to write a DB2 query which will fetch all the members whose :-
1) sum of three columns is zeroes
E.X :- Select all members where sum of ( column 1 + column 2 + colum3) = 0
Thanks,
Tattva |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Wed Jun 08, 2005 4:47 am Post subject: |
|
|
tattva,
Try this
Code: |
SELECT A.*
FROM (SELECT MEMBER
,SUM(COL1 + COL2 + COL3) TOT
FROM TABLE
GROUP BY MEMBER) A
WHERE A.TOT = 0
;
|
Hope this helps...
Cheers
Kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
tattva Beginner
Joined: 02 Feb 2005 Posts: 97 Topics: 36
|
Posted: Thu Jun 09, 2005 1:17 am Post subject: |
|
|
Hi Kolusu,
But this is taking a lot of time.
Let me mention here that primary key on the table are member, ssn and 3 more colums.
Thanks
Tattva |
|
Back to top |
|
 |
kolusu Site Admin

Joined: 26 Nov 2002 Posts: 12378 Topics: 75 Location: San Jose
|
Posted: Thu Jun 09, 2005 8:27 am Post subject: |
|
|
tattva,
I am sorry for misunderstanding your Question. I re-read it once again and I am assuming that you have 5 colums say
Code: |
MEMBER
SSN
COL_AMT1
COL_AMT2
COL_AMT3
|
I assuming that you want all records when
Code: |
COL_AMT1 + COL_AMT2 + COL_AMT3 = 0
|
If that is right then you can use the following SQL
Code: |
SELECT A.*
FROM (SELECT MEMBER
,SSN
,(COL_AMT1 + COL_AMT2 + COL_AMT3) TOT
FROM TABLE) A
WHERE A.TOT = 0
;
|
Hope this helps...
Cheers
kolusu _________________ Kolusu
www.linkedin.com/in/kolusu |
|
Back to top |
|
 |
|
|