开发者

Returning bit for each Row in Sql which exist in Joined Table

开发者 https://www.devze.com 2023-03-26 04:50 出处:网络
I want to return an extra computed column as bit开发者_如何学Go for each rowID that exist as foreign ke in Joined table.for example: Select PId, PName from Part where PId in (Select distinct FkPid in

I want to return an extra computed column as bit开发者_如何学Go for each rowID that exist as foreign ke in Joined table.for example: Select PId, PName from Part where PId in (Select distinct FkPid in joined Part

Result should be like Pid| PName|Bit|

Thanks


Select PId, PName, CAST(CASE WHEN B.fkPid IS NULL THEN 0 ELSE 1 END AS BIT) ExistsOtherTable
from Part A
LEFT JOIN (Select distinct FkPid FROM [joined Part]) B
ON A.PId = B.fkPid


Simply do a left join!!!!

Select PId, PName, cast (isnull(OtherTable.FkPid,0) as bit) as [Bit]  from Part left join 
OtherTable on Part.PId=OtherTable.FkPid
0

精彩评论

暂无评论...
验证码 换一张
取 消