繁体   English   中英

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

[英]Sql - Using boolean logic in the where clause

我有一个看起来像这样的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为零的用户

如果我理解你的问题, 这就是你需要的

  • 在条件@ShowNoSpecialTerms = 0之间放置AND并且它是真正的部分。
  • Else if条件也相同, Else if相同的过程应用于Else if@ShowNoSpecialTerms = 1 AND @ShowNoSpecialTerms = 1及其正文部分之间。
  • 现在在这些语句之间放置OR

解决了它:

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)
        )

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

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

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

暂无
暂无

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

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