Consider i have 3 tables, tableA, tableB and tableC. I want to join tableA with tableB if tableA.flg=1 else with tableC
Thanks
Champions are not Super Humans, they just fight one extra second, when everyone else QUIT. Remember, sometimes one extra second of effort gives you the SUCCESS.......
You could also try to use case when in the select clause to choose one output column from Table A or Table B like below.
select column1 = case when tableA.flg = 1 then tableA.id else tableB.id end, column2 ....
from TableA
left join TableB on ....
left join TableC on ....
Best Regards,
Please mark the replies as answers if they help or unmark if not.
Feedback to us
shubham.mvp
Member
468 Points
156 Posts
Conditional Table Join
Jan 16, 2013 10:53 AM|LINK
Hi All,
Consider i have 3 tables, tableA, tableB and tableC. I want to join tableA with tableB if tableA.flg=1 else with tableC
Thanks
Regards:
Shubham
imobsuz
Participant
1278 Points
195 Posts
Re: Conditional Table Join
Jan 16, 2013 11:13 AM|LINK
Hi,
Try something like this:
select * from TableA as a left join TableB as b on a.Flg = 1 and b.IdA = a.IdA left join TableC as c on a.Flg <> 1 and c.IdA = a.IdAHope this help.
Chen Yu - MS...
All-Star
21584 Points
2493 Posts
Microsoft
Re: Conditional Table Join
Jan 23, 2013 02:06 AM|LINK
Hi,
You could also try to use case when in the select clause to choose one output column from Table A or Table B like below.
select column1 = case when tableA.flg = 1 then tableA.id else tableB.id end, column2 ....
from TableA
left join TableB on ....
left join TableC on ....
Best Regards,
Feedback to us
Develop and promote your apps in Windows Store