正在尝试在WHERE子句中生成大小写表达式(一个值等于多个值)



我希望能够在WHERE子句中生成一个case表达式,该表达式将使用一个变量来确定要应用的标准:

declare @Classification VARCHAR(20)
set @Classification = 'Leaseup/Stabilized' 
select *
from attributes a
where 
if @Classification = 'Leaseup/Stabilized' then a.subgroup8 in ('Lease-up Communities', 'Stabilized 
Communities')
if @Classification = 'Product Type' then a.subgroup6 in ('Freestanding AL/ALZ', 'Rental Continuums')

只需使用常规逻辑:

select a.*
from attributes a
where (@Classification = 'Leaseup/Stabilized' and a.subgroup8 in ('Lease-up Communities', 'Stabilized Communities'
) OR
(@Classification = 'Product Type' and a.subgroup6 in ('Freestanding AL/ALZ', 'Rental Continuums')
)

如果您真的想要Case在中

declare @Classification 
VARCHAR(20)
set @Classification = 
'Leaseup/Stabilized' 
select *
from attributes a
where 
1=  max (Case when @Classification = 
'Leaseup/Stabilized' and a.subgroup8 in 
('Lease-up Communities', 'Stabilized 
Communities') then 1 end) or
1=   max (Case when @Classification = 
'Product Type' and a.subgroup6 in 
('Freestanding AL/ALZ', 'Rental 
Continuums') then 1 end) 

相关内容

  • 没有找到相关文章

最新更新