简体   繁体   English

Where 子句 TSQL 中的 case 表达式

[英]Case expression in Where Clause TSQL

I'm trying to write a case expression in a where clause but I'm having trouble getting the syntax right for what I want to do.我正在尝试在 where 子句中编写一个 case 表达式,但是我在为我想要做的事情找到正确的语法时遇到了麻烦。 In the where clause I want to have the case statement check the column 'IndividualRateOption' and based on that column I want to add to the where clause.在 where 子句中,我想让 case 语句检查列“IndividualRateOption”,并基于该列我想添加到 where 子句中。

--- Get generic rates
            select  case when RateType = 'PRE' then 1
                         when RateType = 'RID' then 2
                         when RateType = 'QUL' then 3 end,
                    BillingEntityId,
                    @GroupID,
                    RateID,
                    TierItemId,
                    RateUnitType,
                    RateUnitValue,
                    RateType,
                    RateTypeEntityID,
                    EffectiveDate,
                    ExpirationDate,
                    Usage,
                    AgeFrom,
                    AgeTo,
                    Gender,
                    PayBrokerCommissions
              from #BillingEntityRates
             where case IndividualRateSelectionOption when 'ANV' then @AnniversaryDate between EffectiveDate and ExpirationDate
                    OR isnull(@AnniversaryDate,@MemberEligibilityDate) between EffectiveDate and ExpirationDate
                    and EntityId is null

if IndividualRateSelectionOption is 'ANV' I want to filter based on "@anniversaryDate between Effective and ExpirationDate and EntityId is null"如果IndividualRateSelectionOption 是'ANV',我想根据“有效和到期日期之间的@anniversaryDate 和EntityId 为空”进行过滤

If IndividualRateSelectionOption is not 'ANV' I want to filter based off "isnull(@AnniversaryDate,@MemberEligibilityDate) between EffectiveDate and ExpirationDate and EntityId is null"如果IndividualRateSelectionOption 不是'ANV',我想根据EffectiveDate 和ExpirationDate 之间的“isnull(@AnniversaryDate,@MemberEligibilityDate) 和EntityId 为空”进行过滤

Above is what I tried so far but the it's complaining about syntax.以上是我迄今为止尝试过的,但它抱怨语法。 Thanks in Advance.提前致谢。

Don't use a case when simpler boolean logic works:不要使用case时,简单的布尔逻辑的工作原理:

where (IndividualRateSelectionOption = 'ANV' and 
       @anniversaryDate between Effective and ExpirationDate and
       EntityId is null
      ) or
      (IndividualRateSelectionOption <> 'ANV' and 
       coalesce(@AnniversaryDate, @MemberEligibilityDate) between EffectiveDate and ExpirationDate and
       EntityId is null
      )

You can filter out the EntityId is null logic:您可以过滤掉EntityId is null逻辑:

where EntityId is null and
      ( (IndividualRateSelectionOption = 'ANV' and 
         @anniversaryDate between Effective and ExpirationDate          
        ) or
        (IndividualRateSelectionOption <> 'ANV' and 
         coalesce(@AnniversaryDate, @MemberEligibilityDate) between EffectiveDate and ExpirationDate and
        )
      )

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

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