开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜