选择案例陈述

时间:2017-11-28 03:54:25

标签: case sql-server-2014

这里我有一些基于acctype的doctype。如下面的sql。

CASE docType 
   WHEN 'I' THEN 'INVOICE' 
   WHEN 'M' THEN 'INVOICE' 
   WHEN 'D' THEN 'DEBIT NOTE' 
   WHEN 'C' THEN 'CREDIT NOTE' 
   WHEN 'P' THEN 'SUPPLIER INVOICE' 
   WHEN 'N' THEN 'SUPPLIER CREDIT NOTE' 
   ELSE '' 
END as docType

我想如何区分,if doctype = I,M,D,C = Customer, else = supplier

我试着这样做

SELECT
    CASE 
       WHEN (docType = 'I' OR docType = 'M' OR docType = 'D' OR docType = 'C') 
          THEN 'CUSTOMER' 
          ELSE 'SUPPLIER' 
    END as accType 
FROM
    AccountDoc

但结果不会过滤acctype

1 个答案:

答案 0 :(得分:0)

试试这个

select (case when (docType = 'I' OR docType = 'M' OR docType = 'D' OR docType = 'C')
    then 'Customer'
    else 'Supplier' end )
    as accType 
from AccountDoc