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 

Problem in join

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


Joined: 12 Jul 2007
Posts: 55
Topics: 23

PostPosted: Thu Sep 11, 2008 1:58 am    Post subject: Problem in join Reply with quote

I want to fetch all USER_IDs for which TYPE_CD is NOT equal to 010. Is the following query correct one:
Code:

SELECT T1.TYPE_CD,T1.AS_OF_DATE,T2.USER_ID FROM TABLE1  T1
  INNER JOIN TABLE2 T2
  ON (T1.BANK_ID = T2.BANK_ID AND
      T1.BRANCH_ID = T2.BRANCH_ID AND
      T1.ACCOUNT_ID = T2.ACCOUNT_ID)
     
  WHERE T2.USER_ID NOT IN (SELECT T2.USER_ID FROM TABLE1  T1
  INNER JOIN TABLE2 T2
  ON (T1.BANK_ID = T2.BANK_ID AND
      T1.BRANCH_ID = T2.BRANCH_ID AND
      T1.ACCOUNT_ID = T2.ACCOUNT_ID)
        WHERE T1.TYPE_CD = '010')
Back to top
View user's profile Send private message
jsharon1248
Intermediate


Joined: 08 Aug 2007
Posts: 291
Topics: 2
Location: Chicago

PostPosted: Thu Sep 11, 2008 11:11 am    Post subject: Reply with quote

I might be missing something, but I don't see the need for the sub-select. Rather than NOT IN, why not use not equal?

Code:
SELECT T1.TYPE_CD,T1.AS_OF_DATE,T2.USER_ID FROM TABLE1 T1
INNER JOIN TABLE2 T2
ON (T1.BANK_ID = T2.BANK_ID AND
T1.BRANCH_ID = T2.BRANCH_ID AND
T1.ACCOUNT_ID = T2.ACCOUNT_ID)
WHERE T1.TYPE_CD <> '010'
Back to top
View user's profile Send private message
Dibakar
Advanced


Joined: 02 Dec 2002
Posts: 700
Topics: 63
Location: USA

PostPosted: Fri Sep 12, 2008 8:23 pm    Post subject: Reply with quote

maybe interview question??

anyway, both appear to be same and answer to the original question is yes.
Back to top
View user's profile Send private message Send e-mail
dohellwithmf
Beginner


Joined: 12 Jul 2007
Posts: 55
Topics: 23

PostPosted: Sun Sep 14, 2008 11:15 pm    Post subject: Reply with quote

I want to exclude all those USER_IDs which have TYPE_CD = '010'. Taking the following scenario:
Code:

USER_ID              TYPE_CD
AAA                      100
AAA                      400
AAA                      010
BBB                      040
BBB                      050
CCC                     015
CCC                     100
DDD                     015
DDD                     010

The above query will fetch all the USER_IDs but I don't want AAA and DDD in my output.
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: Sun Sep 14, 2008 11:47 pm    Post subject: Reply with quote

dohellwithmf,

Unless I am missing something isnt it a simple case of subselect?

Try this untested sql

Code:

SELECT *
  FROM TABLE
 WHERE USER_ID NOT IN (SELECT USER_ID
                         FROM TABLE
                        WHERE TYP_CD = '010')
;


Hope this helps...

Cheers

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


Joined: 12 Jul 2007
Posts: 55
Topics: 23

PostPosted: Mon Sep 15, 2008 12:30 am    Post subject: Reply with quote

Isn't it the same I mentioned in the Question? USER_ID and TYPE_CD belong to two different table so I used join. Am I correct?
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