Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Join Question

Author  Topic 

bi89405
Starting Member

35 Posts

Posted - 2012-04-13 : 09:25:17
I have 3 tables.

Table A, Table B, and Table C.

Table A contains ID, Table B contains ID, and Table C contains IDa, and IDb.

I want to create a link between A and B on ID BUT if A does not have a corresponding ID in B, then go to C link on IDa and use IDb to link to B.

Does that make sense?
Thanks in advance!

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2012-04-13 : 10:55:40
before offering a solution just curious why does this scenario exist in the first place?

<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page

bi89405
Starting Member

35 Posts

Posted - 2012-04-13 : 11:38:35
Table A is coming from one data source and table B is coming from another data source. Table C is the mapping table b/c some account codes in Table A are not the same as those found in Table B. We need Table C to help us determine which IDb field the account maps to. Does that make sense?
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2012-04-13 : 11:59:14
if C is table which represents relationship between A and B why not always use it for linking A and B? Whats the purpose of direct linking between A and B then if referential integrity is not maintained between them always?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Bustaz Kool
Master Smack Fu Yak Hacker

1834 Posts

Posted - 2012-04-13 : 12:14:15
[CODE]select a.*, coalesce(b.Column, b2.Column) as Column
from TableA a
left outer join
TableB b
on a.id = b.id
left outer join
TableC c
on a.id = c.idA
left outer join
TableB b2
on b2.id = c.idB[/CODE]You'll need to coalesce any column you want returned from the B/B2 table.

=================================================
There is a foolish corner in the brain of the wisest man. -Aristotle, philosopher (384-322 BCE)
Go to Top of Page
   

- Advertisement -