如何在SQL Server的视图中使用else-if条件?

时间:2015-10-15 07:24:47

标签: sql tsql sql-server-2008-r2

我知道使用CASE语句可以实现 if 条件:

CASE
    WHEN <condition>
        x
    ELSE
        z
END

但如何为 else-if 条件执行此操作?

像这样(伪代码):

CASE
    WHEN <condition>
        x
    ELSE-IF <condition>
        y
    ELSE
        z
END

截至目前我的代码:

CREATE VIEW [dbo].[v_myview1]
AS
    SELECT 
        ci.aDate, ci.Currency, ci.lim, ci.LimitUS,
        pa.Date as AmtDate,
        pa.Payment as AmtPayment,
        CASE 
            WHEN (ci.aDate - pa.Date) < 0 THEN ci.LimitUS
            --ELSE IF
            --WHEN (ci.lim - pa.Payment) < 0 THEN 'a'
            ELSE 'b'
        END AS Estimate,
    FROM
        dbo.Amt pa
    JOIN 
        dbo.CrInfo ci ON ci.pr_key = pa.pr_key
    JOIN
        dbo.rates tr ON rt.Currency = ci.Currency 
    WHERE 
        ci.mk = 2

1 个答案:

答案 0 :(得分:7)

您可以使用第二种情况:

CASE 
WHEN (ci.aDate - pa.Date) < 0 THEN  ci.LimitUS
WHEN (ci.lim - pa.Payment) < 0 THEN 'a'
ELSE 'b'