如何调整此CASE表达式?

时间:2013-01-30 21:09:00

标签: sql-server-2008 tsql

我有一个包含多个联接的大查询,如下所示。 现在我必须调整此查询。

当前逻辑是:如果AppealOutcome Code = F / D / P,则插入ReAdjustedClaimControlNumber。

我需要将我的逻辑更改为:对于处置F / D / P,当ReAdjustmentId从MonthlyApepalFile为空时,请使用AdjustmentID。如果AdjustmentId也为空,请使用ClaimControlNumber。

任何人都可以抛出任何捷径吗?

Insert into rec.RecoveryClaims (ClaimControlKey, ClaimOutcomeCode, ClaimControlNumber )
select 
             A.ClaimControlKey
             ,ClaimOutcomeCode

            ,CASE   WHEN E.AppealOutcomecode = 'F' THEN E.ReadjustedClaimControlNumber
                    WHEN E.AppealOutcomecode = 'D' THEN E.ReadjustedClaimControlNumber
                    WHEN E.AppealOutcomecode = 'P' THEN E.ReadjustedClaimControlNumber
                    ELSE E.AdjustedClaimControlNumber
                    END  
FROM rec.RecoveryClaims A 
INNER JOIN @ExistingRecoveryClaimControlKey b 
    on a.RecoveryClaimControlKey = b.RecoveryClaimControlKey
INNER JOIN occ.Claims C 
    on c.ClaimControlKey = A.ClaimControlKey
INNER JOIN @OutputRecoveryCases D 
    on D.CaseID = C.CaseId  
INNER JOIN @NewClaimControlNumbers E 
    ON C.CaseId = E.CaseID 
    AND A.ClaimControlKey = E.ClaimControlKey 

1 个答案:

答案 0 :(得分:3)

您可以使用CASE

折叠IN语句
,CASE   WHEN E.AppealOutcomecode IN ('F', 'D', 'P') 
        THEN E.ReadjustedClaimControlNumber
        ELSE E.AdjustedClaimControlNumber
END 

接下来,如果我了解您的要求,下一部分将如下所示:

,CASE   WHEN Disposition IN ('F', 'D', 'P') 
        THEN COALESCE(ReAdjustmentId , AdjustmentID, ClaimControlNumber)
END

编辑:

如果字符串为空而不是NULL,则可以将NULLIFCOALESCE结合使用:

,CASE   WHEN Disposition IN ('F', 'D', 'P') 
        THEN COALESCE(
            NULLIF(ReAdjustmentId, '') , 
            NULLIF(AdjustmentID, ''), 
            NULLIF(ClaimControlNumber, '')
        )
END