我正在尝试在where子句中编写一个case表达式,但是我无法正确设置所需的语法。在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
如果IndividualRateSelectionOption为'ANV',我想基于“ Effective和ExpirationDate之间的@anniversaryDate,而EntityId为null”进行过滤]
如果IndividualRateSelectionOption不是'ANV',我想根据有效日期和有效日期之间的“ isnull(@ AnniversaryDate,@ MemberEligibilityDate)进行过滤并且EntityId为null“
以上是我到目前为止尝试过的,但是它在抱怨语法。在此先感谢。
当更简单的布尔逻辑起作用时,不要使用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
)
您可以过滤掉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
)
)