User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 391,950 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,890 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 1819 | Replies: 8
Reply
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

help with joins

  #1  
Nov 28th, 2005
Situation

table1 has 2 columns: userid and requiredInfo
It will have an entry for every user

table2 has 2 columns: userid and optionalInfo
It will have entries for some users but not others

Problem:
need a statement that returns either

userid, table1.requiredInfo, table2.optionalInfo
--if table2 has an entry
OR
userid, table1.requiredInfo, null
--if table2 does not have an entry
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2005
Posts: 5
Reputation: stoefln is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stoefln stoefln is offline Offline
Newbie Poster

Re: help with joins

  #2  
Nov 29th, 2005
Originally Posted by campkev
Situation

table1 has 2 columns: userid and requiredInfo
It will have an entry for every user

table2 has 2 columns: userid and optionalInfo
It will have entries for some users but not others

Problem:
need a statement that returns either

userid, table1.requiredInfo, table2.optionalInfo
--if table2 has an entry
OR
userid, table1.requiredInfo, null
--if table2 does not have an entry

can be easy done with an outher join:

SELECT table1.userid, table1.requiredInfo, table2.optionalInfo
FROM table1 LEFT OUTER JOIN table2 ON table1.userid = table2.userid
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: help with joins

  #3  
Nov 29th, 2005
whoops, I messed up explaining the situation.
table2 is a many-to-many table with 3 columns, not 2
userid, optionalInfoDesc, OptionalInfoValue



what i need is

if Userid X has an entry in table2 with optionalInfoDesc = Y
return table1.requiredinfo, table2.optionInfoValue

if Userid X does not have an entry in table2 with OptionalInfoDesc = Y
return table1.requiredinfo, null

i tried:
select table1.requiredInfo, table2.optionalInfoValue from table1
left outer join table2 on table1.userid = X and table1.userid = table2.userid and table2.optionalInfoDesc = Y

but that returns a row for every userid, not just userid X

then I tried:
select table1.requiredInfo, table2.optionalInfoValue from table1
inner join table2 on table1.userid = X and table1.userid = table2.userid and table2.optionalInfoDesc = Y

but that only works if table2 has an entry

I tried
select table1.requiredInfo, table2.optionalInfoValue from table1
right outer join table2 on table1.userid = X and table1.userid = table2.userid and table2.optionalInfoDesc = Y

but that returns a row for every entry for userx in table2 not just the row with optionInfo Desc = y

where am I messing up
Reply With Quote  
Join Date: Nov 2005
Location: South Africa
Posts: 24
Reputation: vangraan is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
vangraan vangraan is offline Offline
Newbie Poster

Re: help with joins

  #4  
Nov 29th, 2005
Is it possible to post a screen shot of a diagram of the tables you are using?
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: help with joins

  #5  
Nov 29th, 2005
[IMG=untitled.jpg][/IMG]
examples

storeproc 1 'PreferredName'
returns John, null

storeproc 2 'PreferredName'
returns Joseph, Joe

storeproc 2 'MaidenName'
returns Joseph, null

storeproc 3 'MaidenName'
returns Elizabeth, Smith
Attached Images
File Type: jpg untitled.jpg (23.4 KB, 7 views)
Reply With Quote  
Join Date: Nov 2005
Posts: 5
Reputation: stoefln is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stoefln stoefln is offline Offline
Newbie Poster

Re: help with joins

  #6  
Nov 29th, 2005
i think it should work like that:
select table1.requiredInfo, table2.optionalInfoValue from table1
left outer join table2 on table1.userid = table2.userid
where table1.userid = X and table2.optionalInfoDesc = Y
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: help with joins

  #7  
Nov 30th, 2005
nope, that returns one row for every user, not just the user specified. Any I solved the problem by using that select into a temp table and then doing a select from the temp table.
Reply With Quote  
Join Date: Nov 2005
Posts: 5
Reputation: stoefln is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
stoefln stoefln is offline Offline
Newbie Poster

Re: help with joins

  #8  
Nov 30th, 2005
Originally Posted by campkev
nope, that returns one row for every user, not just the user specified. Any I solved the problem by using that select into a temp table and then doing a select from the temp table.

"where table1.userid = X " does definatelly NOT return any users which have NOT userid = X
Reply With Quote  
Join Date: Jul 2005
Location: Dallas, TX
Posts: 481
Reputation: campkev is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: help with joins

  #9  
Nov 30th, 2005
I wouldn't think so either, but that is what is happening.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 8:59 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC