Sql 如果外部中的语句适用,则为case when
是否可以在外部应用程序中使用if语句?我似乎无法让它工作 这是有效的现有代码:Sql 如果外部中的语句适用,则为case when,sql,if-statement,case,outer-join,Sql,If Statement,Case,Outer Join,是否可以在外部应用程序中使用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 B
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),我希望它看起来像这样:
选择id--
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
SELECT id --<<-- best to add aliases to all columns
FROM residentmemberlease rml
LEFT 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 (l.codeleasestatuscode = '1' AND a.rthDisplayBit='1' OR
l.codeleasestatuscode = '2' AND a.rthDisplayBit='0')
ORDER BY ISNULL(a.rthMoveOutDate, a.rthMoveInDate) DESC
) AS RH;