Sql-server – How to join one table or other depending on a field value

sql serversql-server-2008

I have one table and I'd like to decide which table join depending on the value of a field.
I have the table T and there is a field Type, so if Type=1 then I have to join with Table2 and if Type=2 I have to join with Ta

Best Answer

Short answer, you can't. Maybe you can do something like:

select ...
from t
left join t1
    on t.type = 1
    and t.x = t1.x
left join t2
    on t.type = 2
    and t.x = t1.x

to return something (must be same type) from either t1 or t2 you can use:

select coalesce(t1.y, t2.y) ...