Reputation: 1
I am a complete beginner with SQL, and I have to turn a query like this one:
SELECT col1, col2, col3 FROM db1
LEFT OUTER JOIN (SELECT col1, col2 FROM db2 WHERE some_conditions) AS query1
ON (query1.col1 = col2)
Into a query like this one:
SELECT col1, col2, col3 FROM db1
if col1 = 1
LEFT OUTER JOIN (SELECT col1, col2 FROM db2 WHERE some_conditions) AS query1
ON (query1.col1 = col2)
else
LEFT OUTER JOIN (SELECT col1, col2 FROM db2 WHERE some_other_conditions) AS query1
ON (query1.col1 = col2)
But the latter obviously doesn't work in SQL Server. What would be the proper format for a query like this?
Upvotes: 0
Views: 70
Reputation: 238048
You could use a complex on
clause:
select *
from db1
left join
db2
on db1.col1 = db2.col2
and (
(db1.col1 = 1 and some_conditions)
or (db1.col1 <> 1 and some_other_conditions)
)
Using or
, you can use one codition set in one case, and another condition set in another case.
Upvotes: 1