SQL case语句更新不正确的记录

时间:2018-02-23 10:43:25

标签: sql sql-server case

我要做的是将超过180天的潜在客户分配为值1或L.Age_Change IS NULL AND L._Stage IS NOT NULL

我目前遇到的问题是,在下面的查询中,即使不超过180天,也会更新潜在客户。

要么我真的很厚或者我错过了什么,我似乎无法解决问题所在:

SELECT
U.*,
L.ID AS Lead_ID,
CASE
    WHEN 
        (L.Age_Change < DATEADD(day,-180, GETDATE())) 
        OR (
            L.Age_Change IS NULL 
            AND L._Stage IS NOT NULL
            ) 
        AND (
            L._Stage NOT LIKE '%Not Interested%' 
            OR L._Stage NOT LIKE '%Duplicate%'
            ) 
        AND (
            L.Start_Term NOT LIKE '%September 2019%' 
            OR L.Start_Term NOT LIKE '%September 2020%' 
            OR L.Start_Term NOT LIKE '%September 2021%'
            ) 
    THEN '1'

非常感谢有关该问题的任何指导

2 个答案:

答案 0 :(得分:0)

我已经为第一个OR条件更新了一些逻辑并用第一个条件包装然后应用AND条件,请尝试以下查询,是否在寻找?

SELECT
U.*,
L.ID AS Lead_ID,
CASE WHEN 
    (
        (
            L.Age_Change < DATEADD(day,-180, GETDATE())
        ) 
        OR 
        (
            L.Age_Change IS NULL AND L._Stage IS NOT NULL
        ) 
    )
    AND 
    (
        L._Stage NOT LIKE '%Not Interested%' OR L._Stage NOT LIKE '%Duplicate%'
    ) 
    AND 
    (
        L.Start_Term NOT LIKE '%September 2019%' OR  L.Start_Term NOT LIKE '%September 2020%' OR L.Start_Term NOT LIKE '%September 2021%'
    ) 
THEN '1' 
ELSE '0' 
END

按照以下步骤检查输出:

Test as below steps one by one and check output:

1: L.Age_Change < DATEADD(day,-180, GETDATE())
2: (
            (
                L.Age_Change < DATEADD(day,-180, GETDATE())
            ) 
            OR 
            (
                L.Age_Change IS NULL AND L._Stage IS NOT NULL
            ) 
        )

3: AND  
        (
            L._Stage NOT LIKE '%Not Interested%' OR L._Stage NOT LIKE '%Duplicate%'
        )
and so on....

答案 1 :(得分:0)

当您没有数据可供使用时,很难理解逻辑,但也许您想要考虑将一组条件嵌套到另一个CASE语句中。这样,您只能通过实际满足第一个条件,然后是第二个条件来访问“1”标志。它还可以使调试更容易一些吗?

SELECT
U.*,
L.ID AS Lead_ID,
CASE
WHEN
    ( L.Age_Change < DATEADD(day,-180, GETDATE()) )
        OR
    ( L.Age_Change IS NULL AND L._Stage IS NOT NULL )
        THEN 
            CASE WHEN
                (
                    L._Stage NOT LIKE '%Not Interested%' AND L._Stage NOT LIKE '%Duplicate%'
                ) 
                AND 
                (
                    L.Start_Term NOT LIKE '%September 2019%' AND  L.Start_Term NOT LIKE '%September 2020%' AND L.Start_Term NOT LIKE '%September 2021%'
                )
                    THEN '1'
                ELSE '0'
            END
ELSE '0'
END

我也在你的逻辑中将你的OR改为了。

为了完成关于为每个阶段嵌套CASE语句的观点,以下是上述的替代方法:

SELECT
U.*,
L.ID AS Lead_ID,
CASE
WHEN
    ( L.Age_Change < DATEADD(day,-180, GETDATE()) )
        OR
    ( L.Age_Change IS NULL AND L._Stage IS NOT NULL )
        THEN 
            CASE
                WHEN
                    L._Stage NOT LIKE '%Not Interested%'
                        AND L._Stage NOT LIKE '%Duplicate%'
                    THEN
                        CASE
                            WHEN
                                L.Start_Term NOT LIKE '%September 2019%'
                                    AND  L.Start_Term NOT LIKE '%September 2020%'
                                    AND L.Start_Term NOT LIKE '%September 2021%'
                                THEN '1'
                            ELSE '0'
                        END
                    ELSE '0'
            END
ELSE '0'
END
或者,如果可能的话,另一个解决方案可能是在基表上添加一个标志,而不是进行所有的字符串解释。然后你只需要在旗帜上做一个CASE。例如。当flag = 1然后......但正如我所说,由于我不知道完整的逻辑,所以很难帮助太多。