3

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

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
4

2 回答 2

4

您可以COALESCE()在您的情况下执行此操作,但如果您想CASEWHERE标准中使用,您可以这样做。将匹配条件移到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')

于 2013-08-26T15:31:43.353 回答
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)
于 2013-08-26T15:32:17.667 回答