SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to use inner join inside Select using case?


How to use inner join inside Select using case?

Author
Message
jaquino012
jaquino012
Grasshopper
Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)Grasshopper (23 reputation)

Group: General Forum Members
Points: 23 Visits: 57
Hi Experts,

How can i accomplish this using case within select.
Please see my sample below.


select A.name
,(Case A.id=2 then select C.ID from TableThree C left join A.id=C.id end)
from TableOne A
left join TabletTwo B
on A.id=B.id

best regards
Paulo de Jesus
Paulo de Jesus
Say Hey Kid
Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)Say Hey Kid (670 reputation)

Group: General Forum Members
Points: 670 Visits: 955
I wouldn't nest a select statement like that.
Rather do another outer join:


select A.name
,(Case WHEN A.id = 2 then C.ID ELSE ??? END)
from TableOne A
left join TabletTwo B
on A.id=B.id
LEFT JOIN TableThree C
ON C.id = A.id


Also, replace the "???" with whatever value you want if A.id doesn't = 2

But if you absolutely needed to:


select A.name
,(SELECT Case WHEN A.id = 2 then C.ID ELSE ??? END FROM TableThree C left join TableOne A ON A.id = C.id )
from TableOne A
left join TabletTwo B
on A.id=B.id



Eddie Wuerch
Eddie Wuerch
SSCertifiable
SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)SSCertifiable (6.8K reputation)

Group: General Forum Members
Points: 6815 Visits: 2549
select A.name, case when A.id=2 then C.ID else null end as [ID]
from TableOne A
left join TabletTwo B on A.id=B.id
left join TableThree C on A.id=C.id


or:

select A.name, C.ID -- C.ID will be NULL unless a.id = 2
from TableOne A
left join TabletTwo B on A.id=B.id
left join TableThree C on A.id=C.id AND a.id = 2


or

declare @C_ID int = (SELECT ID FROM TableThree WHERE id = 2); -- will be '2' if row found, NULL if not found
select A.name, case when a.id = 2 then @C_ID else NULL end as [c_id]
from TableOne A
left join TabletTwo B on A.id=B.id


Eddie Wuerch
MCM: SQL
drew.allen
drew.allen
SSC-Dedicated
SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)SSC-Dedicated (39K reputation)

Group: General Forum Members
Points: 39531 Visits: 14359
There are actually a couple more options, but the one that you were likely looking for is called a correlated subquery. The subquery can reference values from the outer query, so there is no need for a join. (NOTE: This query will produce an error if there are multiple rows in TableThree that meet the criteria.)

select A.name
,(select C.ID from TableThree C WHERE A.id=C.id AND A.id=2)
from TableOne A
left join TabletTwo B
on A.id=B.id


Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search