Solved

join 2 tables on differnt joins based on 2 different conditions

Posted on 2011-09-19
3
355 Views
Last Modified: 2012-05-12
when you join 2 tables on differnt joins based on 2 different values, is it possible to consolidate the 2 queries into 1?

select * from a join b on a.id = b.id1 and b.indicator = 0
union
select * from a join b on a.id = b.id2 and b.indicator = 1
0
Comment
Question by:25112
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 7

Accepted Solution

by:
twol earned 425 total points
ID: 36561080
select * from a inner join b
on (a.id = b.id1 and b.indicator = 0) or (a.id = b.id2 and b.indicator = 1 )
0
 
LVL 18

Assisted Solution

by:deighton
deighton earned 75 total points
ID: 36561644
I believe the SQL you gave does exactly what you ask, so the answer is yes
0
 
LVL 5

Author Comment

by:25112
ID: 36562190
works..
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question