We some times we need the conditional joins in the select statement for example if condition 1 matches then we want some different column joins and when condition 2 then we want some different join
in this case we can use CASE statement in our sql query.
For Example :
select
T1.Column1
T1.Column1
,T1.Column2
from
Table1 T1
inner join
from
Table1 T1
inner join
ON
Table2 T2
case when <CONDITION> then T1.<Column1> else T1.<Column2> End
case when <CONDITION> then T1.<Column1> else T1.<Column2> End
= T2.<SomeColumn>
Similarly, we can give case statement in both side of the condition
For Example :
select
T1.Column1
T1.Column1
,T1.Column2
from
Table1 T1
inner join
from
Table1 T1
inner join
ON
Table2 T2
case when <CONDITION> then T1.<Column1> else T1.<Column2> End
case when <CONDITION> then T1.<Column1> else T1.<Column2> End
= case when <CONDITION> then T2.<SomeColumn1> else T2.<SomeColumn2> End
No comments:
Post a Comment