簡體   English   中英

嘗試在 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) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM