简体   繁体   English

Where子句,多组条件

[英]Where clause, multiple sets of conditions

I am trying to produce a single table query result with error payment data filtered out by the conditions below. 我正在尝试生成一个单表查询结果,其中错误付款数据已按以下条件过滤掉。 I am unable to get the query to filter out all of the sets of conditions, only the first 3. I have tried different ordering as well as making the last two condition sets a sub-query. 我无法获取查询以筛选出所有条件集,仅筛选出前3个条件。我尝试了不同的排序方式,还使后两个条件集成为了子查询。

How can I filter out all the below conditions? 如何过滤以下所有条件?

Again, 1,2 and 3 seem to have the desired result.Conditions 4 and 5 function, just not when following conditions 1,2 and 3 同样,1,2和3似乎具有预期的结果。条件4和5起作用,只是在遵循条件1,2和3时不起作用

 select [history].[id] 
            [history].[trans],
            CAST([history].[paid_dt] AS Date),
            CAST([history].[due_dt] AS Date),

     FROM [history]

     LEFT OUTER JOIN [information] ON [history].[id] = [information].[id]

    --Condition 1--
            WHERE NOT  ([history].[trans]  IN
            ('TRSF',
             'TRR',
              'BEG',
             'DTR',
             'LTC',
           ))

             OR
    --Condition 2--
            (CONVERT(date,[paid_dt]) = '2015-12-30'
            AND [trans] = 'ADJE') 

            OR
    -- Condition 3--
            ([history].[paid_dt] = [information].[date]
            AND [history].[trans] LIKE '%ADJE%'
            AND [history].[due_dt] < '2017-01-01')

    --Condition 4--
            OR  
                 (datepart(dd, [paid_dt]) = 8) 
        --assume there is more to this condition--


           OR
    --Condition 5--
                 (CONVERT(date,[history].[paid_dt]) = '2018-01-08' 
             and [history].[trans] = 'MANR'));

Just looking for insight as how to address 只是寻找见识以及解决方法

'NOT' acts only on one condition. “不”仅在一种情况下起作用。 If you don't want any of the other conditions to be returned if true... try surrounding all conditions following the 'NOT' keyword in parentheses. 如果您不希望返回任何其他条件(如果为true),请尝试将所有条件括在括号中的'NOT'关键字之后。 Also, double check where you have your parentheses. 另外,请仔细检查括号的位置。 I'm still seeing an odd number of those maybe due to pasting. 我仍然看到其中的奇数可能是由于粘贴。

WHERE NOT 
    (
        (Condition 1)
         OR
        (Condition 2)
     )

You can also try getting away from the 'NOT' keyword by using <> references instead. 您也可以尝试使用<>引用来摆脱'NOT'关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM