简体   繁体   中英

How to use case Statement in Where Clause in SQLserver

I need my query to check certain conditions based on the Customer's Status

Sample Data:

 Table A A_ID Customer_ID Department Feild Case1 Case2 1 101 XA FX1 Zx1 2 102 YB FX2 ZX2 3 103 ZC FX3 ZX3 
  Table  B ---- 
        B_ID      Customer_ID   Status  Feild  Match_Feild   Case1  Case2 
         1            101         C      Null      A          FX1    ZX1
         2            101       Manual    A        AX         FX0    ZX0

in my case if the status is manual then in the where condition i need to check with Feild alone
else i need to check with other two conditions

where case when Status ='Manual' then a.feild = b.feild else a.Case1 = b.Case1 and a.Case2 = B.case2 end.

I am not sure how to put this in my query

    where (Status = 'Manual' and a.feild = b.feild ) 
          OR 
          (Status<> 'Manual' AND a.Case1 = b.Case1 and a.Case2 = B.case2)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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