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 Question

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


Joined: 27 Dec 2002
Posts: 46
Topics: 15

PostPosted: Thu Nov 09, 2006 1:16 pm    Post subject: Db2 Query Question Reply with quote

I have a following table where owners and accounts are stored. Owner ID and A/C no is the Primary Key.

Code:


Owner_ID   Account_No
O1         A1
O1         A2
O1         A3
O2         A11
O2         A22
O2         A33
O2         A1
O4         A5
O5         A5


In my processing if I know a set of owners , can I automatically pick up all the accounts jointly owned by them ?
For Example if I have owners O1 and O2, can I get only A1 account in a Query?

I tried the Following and it returned two rows and I programmatically looked at each account no and went to the DB2 table to find the owners and compared with owners O1 and O2 and narrowed on one.

Select Account_No, Count(*) from MATSER TABLE
Group by Account_no, having Count(*) > 1
Order by Account_No.

Any help is appreciated.

Thanks
Novice
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 Nov 09, 2006 1:57 pm    Post subject: Reply with quote

Quote:

For Example if I have owners O1 and O2, can I get only A1 account in a Query?


Novice,

I don't see any thing wrong with the query.

Code:

SELECT Account_No                       
  FROM TABLE                           
 GROUP BY Account_No HAVING COUNT(*) > 1
Order by Account_No


This would return just
Code:

A1
A5


You need to post a clear example of your input and desired output

Kolusu
_________________
Kolusu
www.linkedin.com/in/kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
Novice
Beginner


Joined: 27 Dec 2002
Posts: 46
Topics: 15

PostPosted: Thu Nov 09, 2006 2:35 pm    Post subject: Reply with quote

Kolusu,

Thanks for your reply and time.

This query is a part of a process, where in I will get specific owners and I need to find the associated Joint account existence.

As per my current process, due to my query I will get both A1 and A5, then I will pick up each account and will go to table again and pick up the owners and compare them with my input owners.

For example if my process gives me Owner Id as O4 and O5, here are my steps

1) Run above Query - Store result in a table ( 2 in this case)
2) Perform Result till end of table
a) Pick up 1st result i.e., Account No , go to table and get owners - This will return O1 and O2.
b) Compare these owners with my input owner Id's O4 and O5, they are not same, then ignore and go to next account.
c) For the next account , go to table and get the owners - This will return O4 and O5.
d) Compare these owners with my input O4 and O5, they are same, and there is an existence of Joint account for these owners and stop the above loop.

Please note even though two owners can have multiple accounts, I am interested only one of them (Like in the above example if O1 and O2 also own A6, I am interested to know whether there is a joint account exists or not for O1 and O2)

Thanks Once again
Novice
Back to top
View user's profile Send private message
sriramla
Beginner


Joined: 22 Feb 2003
Posts: 74
Topics: 1

PostPosted: Fri Nov 10, 2006 1:00 pm    Post subject: Reply with quote

Try this untested code:
Code:
SELECT
   OWNER_ID, ACCOUNT_NO
FROM
   MASTER_TABLE A, MASTER_TABLE B
WHERE
          A.OWNER_ID = 'O1'
     AND  B.OWNER_ID = 'O2'
     AND  A.ACCOUNT_NO = B.ACCOUNT_NO;
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