SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Question on multiple joins
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

darms21
Yak Posting Veteran

53 Posts

Posted - 04/24/2012 :  10:58:02  Show Profile  Reply with Quote
I have a table called asset_owner which stores a serial number of an asset and an owner's name. I also have 2 additional tables called related_owner and related_user. How can I join the owner's name from asset_owner to related_owner if a match exist but use related_user if a match doesn't?
I want my output to have 2 columns...serialnumber and owner name.

Any ideas?

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 04/24/2012 :  11:08:25  Show Profile  Reply with Quote
Would need to join the three tables, and use coalesce:
SELECT
	ao.serialnumber,
	COALESCE(ao.owner_name,ro.name,ru.name) AS OwnerName
FROM
	asset_owner ao
	LEFT JOIN related_owner ro ON ro.serialnumber = ao.serialnumber
	LEFT JOIN related_user ru ON ru.serialnumber = ao.serialnumber;
Go to Top of Page

vinu.vijayan
Posting Yak Master

India
227 Posts

Posted - 04/27/2012 :  06:25:42  Show Profile  Reply with Quote
If Sunita's Query is not what you are looking for then please be more elaborate about your requirement.

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000