简体   繁体   English

如何在SQLserver中的Where子句中使用case语句

[英]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. 如果状态为“手动”,则a.feild = b.feild,否则a.Case1 = b.Case1和a.Case2 = B.case2结尾。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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