<>不等于函数不能正确地过滤SQL Server中的记录

时间:2014-08-22 15:04:31

标签: sql sql-server reporting-services

我有业务需求,我需要根据一些额外的过滤来更改SSRS报告。我有一个字段名称为ProductShortName,他们不希望记录产品名称为'BLOC','Small Business Visa',产品名称以'WOW'开头,产品名称以'Review'结尾。

这是原来的条件:

       WHERE ( A.AppDetailSavePointID = 0) AND (B.QueueID = 1) 
              AND (A.DecisionStatusName <> N'Cancelled') 
              AND (A.DecisionStatusName <> N'Withdrawn') 
              OR  (A.AppDetailSavePointID = 0) 
              AND  ((B.QueueID = - 25) OR (B.QueueID = - 80)) 
              AND (A.DecisionStatusName <> N'Cancelled') 
              AND (A.DecisionStatusName <> N'Withdrawn') 
              OR  (A.AppDetailSavePointID = 0) 
              AND (A.DecisionStatusName <> N'Cancelled') 
              AND (A.DecisionStatusName <> N'Withdrawn') 
              AND (LEFT(C.QueueName, 2) = 'LC')

我添加了额外的过滤以符合标准:

 WHERE (A.AppDetailSavePointID = 0) 
        AND ((A.ProductShortName <> 'BLOC') 
        AND (A.ProductShortName <> 'Small Business Visa') 
        AND NOT (A.ProductShortName LIKE 'WOW%') 
        AND NOT (A.ProductShortName LIKE '%Review')) 
        AND (B.QueueID = 1) 
        AND (A.DecisionStatusName <> N'Cancelled') 
        AND (A.DecisionStatusName <> N'Withdrawn') 
        OR (A.AppDetailSavePointID = 0) 
        AND ((B.QueueID = - 25) OR (B.QueueID = - 80)) 
        AND (A.DecisionStatusName <> N'Cancelled') 
        AND (A.DecisionStatusName <> N'Withdrawn') 
        AND ((A.ProductShortName <> 'BLOC') 
        AND (A.ProductShortName <> 'Small Business Visa') 
        AND NOT (A.ProductShortName LIKE 'WOW%') 
        AND NOT (A.ProductShortName LIKE '%Review')) 
        AND (A.AppDetailSavePointID = 0) 
        AND (A.DecisionStatusName <> N'Cancelled') 
        AND (A.DecisionStatusName <> N'Withdrawn') 
        AND (LEFT(C.QueueName, 2) = 'LC') 
        AND ((A.ProductShortName <> 'BLOC') 
        AND (A.ProductShortName <> 'Small Business Visa') 
        AND NOT (A.ProductShortName LIKE 'WOW%') 
        AND NOT (A.ProductShortName LIKE '%Review'))

虽然这会删除产品,但它还会删除更多产品。我不明白怎么回事?任何人都可以建议适当的条件吗?

3 个答案:

答案 0 :(得分:4)

您应该避免混合使用AND和OR条件而不将它们正确包围。 如果您正在混合AND和OR,则使用括号来解决混淆。如果你不这样做,结果将是意料之外的。

例如,在您的查询中,如果AppDetailSavePointID = 0,则所有其他条件将变为无效/无关。我确定这不是你想要的。

   WHERE (AppDetailSavePointID = 0) AND (QueueID = 1) 
          AND (DecisionStatusName <> N'Cancelled') 
          AND (DecisionStatusName <> N'Withdrawn') 
          OR  (AppDetailSavePointID = 0) 
          AND  ((QueueID = - 25) OR (QueueID = - 80)) 
          AND (DecisionStatusName <> N'Cancelled') 
          AND (DecisionStatusName <> N'Withdrawn') 
          OR  (AppDetailSavePointID = 0) 
          AND (DecisionStatusName <> N'Cancelled') 
          AND (DecisionStatusName <> N'Withdrawn') 
          AND (LEFT(QueueName, 2) = 'LC')

修改

您应该使用AND或OR作为主要部分,但不能使用AND和OR(不带括号)的混合。您可以使用额外的括号来指定另一个。

e.g。

假设a,b,c,d,e,f ......是类型Field op value的条件(例如AppDetailSavePointID = 0,DecisionStatusName&lt;&gt; N'Cancelled'等)。

你不应该这样做:

-- don't do this.
WHERE a
AND b
OR c
AND d 
OR e
AND f 
OR g

你可以做以下两件事之一:

-- this is ok.
WHERE a
AND b
AND c
AND (d OR e)
AND (f OR g)

或者,

-- this is ok.
WHERE a
OR b
OR c
OR (d AND e)
OR (f AND g)

答案 1 :(得分:1)

如果取消关联通用谓词,可能更容易阅读。

(X and Y) or (X and Z) == X and (Y or Z)

这会产生:

WHERE (A.ProductShortName NOT LIKE 'WOW%')
  AND (A.ProductShortName NOT LIKE '%Review')
  AND (A.ProductShortName <> 'Small Business Visa')
  AND (A.DecisionStatusName <> N'Cancelled')
  AND (A.DecisionStatusName <> N'Withdrawn')
  AND (A.AppDetailSavePointID = 0)
  AND (   QueueID = 1
       OR QueueID = -25
       OR QueueID = -80
       OR LEFT(C.QueueName, 2) = 'LC'
      )

答案 2 :(得分:0)

如果原始状态正常,我只会添加它。请尝试以下方法:

WHERE 
    (AppDetailSavePointID = 0) AND (QueueID = 1) 
    AND (DecisionStatusName <> N'Cancelled') 
    AND (DecisionStatusName <> N'Withdrawn') 
    OR  (AppDetailSavePointID = 0) 
    AND  ((QueueID = - 25) OR (QueueID = - 80)) 
    AND (DecisionStatusName <> N'Cancelled') 
    AND (DecisionStatusName <> N'Withdrawn') 
    OR  (AppDetailSavePointID = 0) 
    AND (DecisionStatusName <> N'Cancelled') 
    AND (DecisionStatusName <> N'Withdrawn') 
    AND (LEFT(QueueName, 2) = 'LC')
    AND NOT (
        ProductShortName IN ('BLOC', 'Small Business Visa')
        OR ProductShortName LIKE 'WOW%'
        OR ProductShortName LIKE '%Review'
    )