Sql - 在where子句中使用布尔逻辑

时间:2011-08-18 11:52:57

标签: sql sql-server tsql

我有一个看起来像这样的where子句

WHERE   
    OnSideOffSideTotal  < Isnull(@OnsideOffsideUpper, 9999999999999)
    AND OnSideOffSideTotal  > Isnull(@OnsideOffsideLower, -9999999999999)
    AND GbpExposureClientTotal < Isnull(@GBPExposureUpper, 9999999999999)
    AND GbpExposureClientTotal > Isnull(@GBPExposureLower, -9999999999999)
    AND UsedTolerancePercentage < Isnull(@UsedToleranceUpper, 9999999999999)
    AND UsedTolerancePercentage > Isnull(@UsedToleranceLower, -9999999999999)

它基本上允许为报告指定ceratin范围内的过滤 如果省略值,则默认为非常大的正数或负数

我遇到的问题是某些用户没有定义Tolerance,这意味着他们的Tolerance百分比将始终为零,

所以如果报告指定了这样的标准

set @GBPExposureUpper = -7500
    set @OnsideOffsideUpper = -3
    set @UsedToleranceUpper = -100

然后它不包括没有定义公差的用户(因为他们的公差将为零 - 显然大于-100)

所以我创建了一个位字段(@ShowNoSpecialTerms),它由报表上的一个复选框控制,该复选框设置一个标志,表示我们想要附加地包含没有定义公差的用户

如果这是假的,我们应该正常使用过滤条件

如果确实如此,我们还应该包括未定义容差的用户 - 可以使用ToleranceLevel列标识这些用户

所以逻辑有点像

if (@ShowNoSpecialTerms = 0)

    OnSideOffSideTotal  < Isnull(@OnsideOffsideUpper, 9999999999999)
    AND OnSideOffSideTotal  > Isnull(@OnsideOffsideLower, -9999999999999)
    AND GbpExposureClientTotal < Isnull(@GBPExposureUpper, 9999999999999)
    AND GbpExposureClientTotal > Isnull(@GBPExposureLower, -9999999999999)
    AND UsedTolerancePercentage < Isnull(@UsedToleranceUpper, 9999999999999)
    AND UsedTolerancePercentage > Isnull(@UsedToleranceLower, -9999999999999)

...

if  (@ShowNoSpecialTerms = 1)
--then additionally bring back result where ToleranceLevel=0

尝试了以下

WHERE   
    OnSideOffSideTotal  < Isnull(@OnsideOffsideUpper, 9999999999999)
    AND OnSideOffSideTotal  > Isnull(@OnsideOffsideLower, -9999999999999)
    AND GbpExposureClientTotal < Isnull(@GBPExposureUpper, 9999999999999)
    AND GbpExposureClientTotal > Isnull(@GBPExposureLower, -9999999999999)
    AND (@ShowNoSpecialTerms = 0 OR
        UsedTolerancePercentage < Isnull(@UsedToleranceUpper, 9999999999999)
        AND UsedTolerancePercentage > Isnull(@UsedToleranceLower, -9999999999999))

这是我想要的一半 - 基本上当@ShowNoSpecialTerms = 1忽略剩余的公差检查时

然而,我想要的是它返回结果或公差检查PLUS那些ToleranceLevel为零的用户

3 个答案:

答案 0 :(得分:1)

如果我理解你的问题,this is what you need

  • 在条件@ShowNoSpecialTerms = 0和它之间放置 AND 真实的部分。
  • 将相同的程序应用于Else if条件,即放置 AND @ShowNoSpecialTerms = 1与其身体部位之间。
  • 现在在这些陈述之间放置 OR

答案 1 :(得分:1)

解决了它:

WHERE   
    OnSideOffSideTotal  < Isnull(@OnsideOffsideUpper, 9999999999999)
    AND OnSideOffSideTotal  > Isnull(@OnsideOffsideLower, -9999999999999)
    AND GbpExposureClientTotal < Isnull(@GBPExposureUpper, 9999999999999)
    AND GbpExposureClientTotal > Isnull(@GBPExposureLower, -9999999999999)
    AND ( 
            -- it falls in this tolerance range
            (UsedTolerancePercentage < Isnull(@UsedToleranceUpper, 9999999999999) AND UsedTolerancePercentage > Isnull(@UsedToleranceLower, -9999999999999)) 
            -- OR, if it falls outside of the range but the tolerance is zero AND we have asked these results to be additionally included
            OR(@ShowNoSpecialTerms = 1 AND ToleranceLevel = 0)
        )

答案 2 :(得分:0)

这还不太清楚,但我认为你想要:

WHERE (normal conditions)
OR (@ShowNoSpecialTerms = 1 AND
    <Additional logic for those special users>).

只有在该场景中翻转该位时才会使用OR