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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | 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)
 conditional joins in select statement
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

svibuk
Yak Posting Veteran

62 Posts

Posted - 02/19/2014 :  07:38:22  Show Profile  Reply with Quote
SELECT t1.id,name, from table1 t1 INNER join table t2 on t1.id=t2.pid
case when type='A' then
Inner join table3 t3 on t1.id=t3.id
else
Inner join table4 t4 on t1.id=t4.id
END

Edited by - svibuk on 02/19/2014 07:40:13

stepson
Aged Yak Warrior

Romania
545 Posts

Posted - 02/19/2014 :  07:59:18  Show Profile  Reply with Quote

SELECT t1.id,name, from table1 t1 
	INNER join table t2 on t1.id=t2.pid
	
	LEFT JOIN table3 t3 on t1.id=t3.id AND TYPE='A'
	LEFT JOIN table4 t4 on t1.id=t4.id AND TYPE<>'A'





sabinWeb MCP
Go to Top of Page

svibuk
Yak Posting Veteran

62 Posts

Posted - 02/19/2014 :  23:51:59  Show Profile  Reply with Quote
quote:
Originally posted by stepson


SELECT t1.id,name, from table1 t1 
	INNER join table t2 on t1.id=t2.pid
	
	LEFT JOIN table3 t3 on t1.id=t3.id AND TYPE='A'
	LEFT JOIN table4 t4 on t1.id=t4.id AND TYPE<>'A'


cant use it in this way
table3 t3
table4 t4
both this tables can have same id but the type will be different

eg :

id Name Type
1 abc A
2 xyz A
1 mno D
3 bbb A


the data can be in this way
sabinWeb MCP

Go to Top of Page

marcusn25
Yak Posting Veteran

Germany
56 Posts

Posted - 03/04/2014 :  18:37:19  Show Profile  Reply with Quote
Would this not solve it for you?


SELECT t1.id,name, from table1 t1
INNER join table t2 on t1.id=t2.pid

LEFT JOIN table3 t3 on t1.id=t3.id AND t1.TYPE='A' AND t3.TYPE='A'
LEFT JOIN table4 t4 on t1.id=t4.id AND t1.TYPE<>'A' AND t4.TYPE<>'A'

Marcus

I learn something new everyday.
Go to Top of Page
  Previous Topic Topic Next 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