尝试在 WHERE 子句中使用 case 表达式(一个值等于多个)
Trying to make case expression in WHERE clause (one value equals multiple)
我希望能够在 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)
我希望能够在 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)