943,749 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 1076
  • MS SQL RSS
Jan 24th, 2008
0

Joins

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
bhakti.thakkar is offline Offline
14 posts
since Sep 2007
Jan 24th, 2008
0

Re: Joins

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
MS SQL Syntax (Toggle Plain Text)
  1. SELECT b.id, isnull(a.name,b.name) AS [Name] FROM b
  2. LEFT JOIN a ON b.id = a.id
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Jan 24th, 2008
0

Re: Joins

Or maybe:

MS SQL Syntax (Toggle Plain Text)
  1. SELECT b.id, isnull(a.name,b.name) AS [Name] FROM b
  2. LEFT JOIN a ON b.[Name] = LEFT(a.[Name],1)
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jan 24th, 2008
0

Re: Joins

that would add the assumption that the names in b are the first letter of the names in a
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
Jan 25th, 2008
0

Re: Joins

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.
Reputation Points: 262
Solved Threads: 68
Veteran Poster
hollystyles is offline Offline
1,181 posts
since Feb 2005
Jan 28th, 2008
0

Re: Joins

Click to Expand / Collapse  Quote originally posted by campkev ...
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
MS SQL Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
bhakti.thakkar is offline Offline
14 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: StoredProcedure @variable = table.field not working
Next Thread in MS SQL Forum Timeline: Wierd maintenance plan problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC