不像SQL中的语句不起作用

时间:2018-02-16 19:03:38

标签: sql sql-server 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) 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' 

WHEN (L._Stage IS NULL AND L.Created_Date < DATEADD(day,-45, GETDATE()) 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 '2'

WHEN ((L.Age_Last__Stage_Change < DATEADD(day,-180, GETDATE())) OR 
(L.Age_Last__Stage_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 LIKE '%September 2019%' OR 
 L.Start_Term LIKE '%September 2020%' OR 
 L.Start_Term LIKE '%September 2021%')) THEN '3'
 ELSE NULL END AS Case_Logic

 FROM
 TABLE_A AS U

我用过&lt;&gt;和!=但两个都不起作用。任何有关该问题的指导将不胜感激

3 个答案:

答案 0 :(得分:1)

&#39;不感兴趣&#39;将始终与您的第一个案例匹配,因为它与L._Stage NOT LIKE '%Duplicate%'匹配并且您使用了OR。尝试将其更改为L._Stage NOT LIKE '%Not Interested%' AND L._Stage NOT LIKE '%Duplicate%'

答案 1 :(得分:1)

(L._Stage NOT LIKE '%Not Interested%' OR L._Stage NOT LIKE '%Duplicate%')

这可能永远是正确的,除非你的_stage为'Not Interested, Duplicate'。当您反转LIKE时,您还必须反转AND/OR逻辑,因此您的逻辑与之相反:

(L._Stage LIKE '%Not Interested%' AND L._Stage LIKE '%Duplicate%')

您的开始条款存在同样的问题。或者与NOT结合将匹配,除非所有三个起始术语都在同一列中,基于列名称我希望它只是一个,一种开始日期,这意味着它将匹配所有内容。

所以你真正想要的可能是:

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%'
  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' 

您的包围可能会出现更多问题,包括许多冗余括号,具体取决于您希望应用l.AgeChange的OR,您需要:

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%'
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'

要求年龄变化超过180天或无效,所有其他条件仍然重要或:

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%'
  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' 

如果年龄变化> 180天总是1,否则如果年龄变化为空,请检查其他条件。

答案 2 :(得分:0)

您真的想要检查您的AND和OR逻辑,因为这是您的问题所在。一个可能的提示是,只需制作一小段代码片段来测试可能的情况,看看哪些看起来是正确的,哪些看起来不正确。

顶部查询与您发布的代码相似,底部是用AND交换OR。

SELECT *
  FROM
     (
        SELECT 'NOT INTERESTED' as _Stage, 'DECEMBER 2017'  AS Start_Term
        UNION ALL
        SELECT 'DUPLICATE', 'NOVEMBER 2017'
        UNION ALL
        SELECT 'OTHER VALUE', 'JANUARY 2018'
        UNION ALL
        SELECT 'VALUE 1', 'SEPTEMBER 2019'
        UNION ALL
        SELECT 'VALUE 2', 'SEPTEMBER 2020'
        UNION ALL
        SELECT 'VALUE 3', 'SEPTEMBER 2021'
     ) AS L
 WHERE (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%');

SELECT *
  FROM
     (
        SELECT 'NOT INTERESTED' as _Stage, 'DECEMBER 2017'  AS Start_Term
        UNION ALL
        SELECT 'DUPLICATE', 'NOVEMBER 2017'
        UNION ALL
        SELECT 'OTHER VALUE', 'JANUARY 2018'
        UNION ALL
        SELECT 'VALUE 1', 'SEPTEMBER 2019'
        UNION ALL
        SELECT 'VALUE 2', 'SEPTEMBER 2020'
        UNION ALL
        SELECT 'VALUE 3', 'SEPTEMBER 2021'
     ) AS L
 WHERE (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%');