在WHERE子句中使用CASE - 数据参数Sql Server

时间:2013-08-26 15:22:55

标签: sql sql-server

我正在尝试验证该示例,如果日期为空,则从另一个字段中选择一个日期。查询下方:

SELECT
    *
FROM
    OrderPublish OP
    INNER JOIN Advertising AD ON
        AD.IdOrderPublish = OP.IdOrderPublish
    INNER JOIN Client Cli ON
        Cli.IdClient = OP.IdClient
    LEFT JOIN AdvertisingInserted AII ON
        AD.IdAdvertisingInserted = AII.IdAdvertisingInserted
    LEFT JOIN TypeProduct TP ON
        AII.IdTypeProduct = TP.IdTypeProduct
    INNER JOIN Publication PUB ON
        PUB.IdAdvertisingInserted = AII.IdAdvertisingInserted
WHERE
    TP.IdTypeProduct in (5,7)
    AND CASE WHEN PUB.DtIni IS NULL
        THEN
            OP.DtInc >= Isnull('08/18/2013', OP.DtInc)
        ELSE
            PUB.DtIni >= Isnull('08/18/2013', PUB.DtIni)
        END
    AND CASE PUB.DtFinal WHEN NULL
        THEN
            OP.DtInc <= Isnull('08/23/2013', OP.DtInc)
        ELSE
            PUB.DtFinal <= Isnull('08/23/2013', PUB.DtFinal)
        END

2 个答案:

答案 0 :(得分:4)

您可以在案例中使用COALESCE()执行此操作,但如果您想在CASE条件中使用WHERE,则可以执行此操作。将匹配条件移到CASE语句之外,需要CASE语句来返回要比较的值,而不是比较本身。这需要将每个CASE语句加倍:

SELECT
    *
FROM
    OrderPublish OP
    INNER JOIN Advertising AD ON
        AD.IdOrderPublish = OP.IdOrderPublish
    INNER JOIN Client Cli ON
        Cli.IdClient = OP.IdClient
    LEFT JOIN AdvertisingInserted AII ON
        AD.IdAdvertisingInserted = AII.IdAdvertisingInserted
    LEFT JOIN TypeProduct TP ON
        AII.IdTypeProduct = TP.IdTypeProduct
    INNER JOIN Publication PUB ON
        PUB.IdAdvertisingInserted = AII.IdAdvertisingInserted
WHERE
    TP.IdTypeProduct in (5,7)
    AND CASE WHEN PUB.DtIni IS NULL
        THEN
            OP.DtInc
        ELSE
            PUB.DtIni 
        END
    >=  CASE WHEN PUB.DtIni IS NULL
        THEN
            Isnull('08/18/2013', OP.DtInc)
        ELSE
            Isnull('08/18/2013', PUB.DtIni)
        END

    AND CASE PUB.DtFinal WHEN NULL
        THEN
            OP.DtInc 
        ELSE
            PUB.DtFinal 
        END
    <= CASE PUB.DtFinal WHEN NULL
        THEN
            Isnull('08/23/2013', OP.DtInc)
        ELSE
            Isnull('08/23/2013', PUB.DtFinal)
        END    

您可以在CASE条件中使用WHERE语句,但可以使用AND ()OR ()条件以及COALESCE()来执行此操作没有CASE

此外,您的ISNULL()语句是向后的,'08/18/2013'永远不会是NULL,因此该字段不会被评估,您可能意味着:Isnull(OP.DtInc,'08/23/2013')

答案 1 :(得分:2)

您可以尝试使用Coalesce函数代替大小写:http://msdn.microsoft.com/en-us/library/ms190349.aspx

SELECT
    *
FROM
    OrderPublish OP
    INNER JOIN Advertising AD ON
        AD.IdOrderPublish = OP.IdOrderPublish
    INNER JOIN Client Cli ON
        Cli.IdClient = OP.IdClient
    LEFT JOIN AdvertisingInserted AII ON
        AD.IdAdvertisingInserted = AII.IdAdvertisingInserted
    LEFT JOIN TypeProduct TP ON
        AII.IdTypeProduct = TP.IdTypeProduct
    INNER JOIN Publication PUB ON
        PUB.IdAdvertisingInserted = AII.IdAdvertisingInserted
WHERE
    TP.IdTypeProduct in (5,7)
    AND Coalesce(PUB.DtIni,OP.DtInc)>= Isnull('08/18/2013', OP.DtInc)
    AND Coalesce(PUB.DtFinal,OP.DtInc)<= Isnull('08/23/2013', OP.DtInc)