外部適用内の if ステートメントを持つことは可能ですか? 私はそれを機能させることができないようです。
これは動作する既存のコードです:
select id
from
residentmemberlease rml
left outer join lease l on rml.leaid = l.leaid
outer apply
(SELECT TOP(1) * FROM dbo.RentalHistory a WITH(NOLOCK) WHERE a.resmID = RML.resmID and rthDisplayBit='1'
ORDER BY ISNULL(a.rthMoveOutDate, a.rthMoveInDate) DESC) AS RH
codeleasestatuscode の値 (1 または 2 の場合) に応じて、次のように表示します。
select id
from
residentmemberlease rml
left outer join lease l on rml.leaid = l.leaid
outer apply
if(select codeleasestatuscode from lease) = '1'
begin
(SELECT TOP(1) * FROM dbo.RentalHistory a WITH(NOLOCK) WHERE a.resmID = RML.resmID and rthDisplayBit='1'
ORDER BY ISNULL(a.rthMoveOutDate, a.rthMoveInDate) DESC) AS RH
end
if(select codeleasestatuscode from lease) = '2'
begin
(SELECT TOP(1) * FROM dbo.RentalHistory a WITH(NOLOCK) WHERE a.resmID = RML.resmID and rthDisplayBit='0'
ORDER BY ISNULL(a.rthMoveOutDate, a.rthMoveInDate) DESC) AS RH
end