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 

Help in SQL query

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


Joined: 04 Feb 2003
Posts: 113
Topics: 37

PostPosted: Wed Aug 27, 2003 7:24 am    Post subject: Help in SQL query Reply with quote

Hi ,
I have a table with Account_id and bank_no. Sample data is given below.

Code:


Account_id           Bank_no
------------           ----------
1                        115
1                        294
2                        115
2                        294   
3                        294
4                        115
5                        110
5                        112

1. I need to get all the Account_id which are common in both 115 and 294
Bank_no.
Result : 1 , 2
2. I have to get all the account_id which are unique for bank_no 294.. Means which are present in only for bank_no 294.
Result : 3

Can somebody help me on this query..?
Thanks
-Somu
Back to top
View user's profile Send private message Yahoo Messenger
CZerfas
Intermediate


Joined: 31 Jan 2003
Posts: 211
Topics: 8

PostPosted: Wed Aug 27, 2003 7:52 am    Post subject: Reply with quote

Code:

1)  SELECT T1.Account_id
        FROM your_table T1
                ,your_table T2
      WHERE T1.Bank_no = T2.Bank_no
          AND T1.Account_id = T2.Account_id
          AND T1.Bank_no in (115, 294);

Code:

2)  SELECT T1.Account_id
        FROM your_table T1
      WHERE T1.Bank_no = 294
          AND NOT EXISTS
                 (SELECT 1
                     FROM your_table T2
                   WHERE T2.Bank_no <> 294
                       AND T2.Account_id = T1.Account_id
                 );

regards
Christian
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


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

PostPosted: Wed Aug 27, 2003 8:37 am    Post subject: Reply with quote

Somu,

Another way of getting the desired results.

1.
Code:

SELECT ACCOUNT_ID                           
  FROM TABLE                           
 WHERE BANK_NO IN ('115','294')       
 GROUP BY ACCOUNT_ID HAVING COUNT(*) > 1     
 ;                                           


2.
Code:

SELECT ACCOUNT_ID                 
  FROM TABLE                 
 WHERE ACCOUNT_ID NOT IN  (SELECT ACCOUNT_ID                   
                             FROM TABLE
                            WHERE BANK_NO <> '294')
   AND BANK_NO = '294'               
 ;                                 


Hope this helps...

cheers

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


Joined: 04 Feb 2003
Posts: 113
Topics: 37

PostPosted: Thu Aug 28, 2003 12:16 am    Post subject: Reply with quote

Thanks Christian and Kolusu..

Regds
-Somu
Back to top
View user's profile Send private message Yahoo Messenger
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