Joins

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 14
Reputation: bhakti.thakkar is an unknown quantity at this point 
Solved Threads: 1
bhakti.thakkar bhakti.thakkar is offline Offline
Newbie Poster

Joins

 
0
  #1
Jan 24th, 2008
Hi
can any one help me in following case?
Table A

ID | Name
1 , Apple
2 , Bat
3 , Cat
4 , Dog


Table B
ID | Name

1 , A
2 , B
3 , C
4 , D
5 , E
6 , F
7 , G

How will i get the below result??

Results

ID | Name

1 , Apple
2 , Bat
3 , Cat
4 , Dog
5 , E
6 , F
7 , G
How can I achieve this.
Thanks
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Joins

 
0
  #2
Jan 24th, 2008
ok, i am assuming that you want a name for all id's. if a has an entry use that, otherwise use the name from b. also assuming that B has an entry for every id. if those assumptions are correct, then this should work
  1. SELECT b.id, isnull(a.name,b.name) AS [Name] FROM b
  2. LEFT JOIN a ON b.id = a.id
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Joins

 
0
  #3
Jan 24th, 2008
Or maybe:

  1. SELECT b.id, isnull(a.name,b.name) AS [Name] FROM b
  2. LEFT JOIN a ON b.[Name] = LEFT(a.[Name],1)
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Joins

 
0
  #4
Jan 24th, 2008
that would add the assumption that the names in b are the first letter of the names in a
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 1,181
Reputation: hollystyles will become famous soon enough hollystyles will become famous soon enough 
Solved Threads: 67
hollystyles's Avatar
hollystyles hollystyles is offline Offline
Veteran Poster

Re: Joins

 
0
  #5
Jan 25th, 2008
Hmm not really, it replaces the assumption that a.id is a foreign key to b.id.

The OP does not specify. If there is a relation between the id's (which there should be in my opinion, and if the data provided represents all possibilities, then this is indeed the case) your solution is the more efficient.

However if the id's are not related then that only leaves the initial letters, in which case my alternate solution will do.
==========================================
Yadda yadda yadda...
Web junky, fevered monkey
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 14
Reputation: bhakti.thakkar is an unknown quantity at this point 
Solved Threads: 1
bhakti.thakkar bhakti.thakkar is offline Offline
Newbie Poster

Re: Joins

 
0
  #6
Jan 28th, 2008
Originally Posted by campkev View Post
ok, i am assuming that you want a name for all id's. if a has an entry use that, otherwise use the name from b. also assuming that B has an entry for every id. if those assumptions are correct, then this should work
  1. SELECT b.id, isnull(a.name,b.name) AS [Name] FROM b
  2. LEFT JOIN a ON b.id = a.id

Thanks to reply my query. It worked.
Last edited by bhakti.thakkar; Jan 28th, 2008 at 12:36 am.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for MS SQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC