带条件的CASE语句MYSQL

时间:2017-01-21 08:20:29

标签: mysql sql

我收到2个CASE声明:第一个CASE声明如下:

Case When ((cust_shipmentdate_awb Is Null OR cust_shipmentdate_awb = '') 
AND (comp_shipdate_awb IS NULL OR comp_shipdate_awb = '')) Then 'Pending'
ELSE 'Shipped' End AS shipment_status

第二个CASE声明如下:

Case When apbg_bal_pay_amt ='0' Then 'Received'
    Else 'Pending' End AS payment_status

我想再写一个名为OVERALL_Status的CASE语句。这基本上是这个CASES(shipment_status和payment_status)的组合,这意味着如果Shipment状态为'Shipped'且Payment_Status为'Received',则Overall_status为'Completed',否则为'Not Completed'。任何人都可以帮我这个。我真的很震惊。我尝试了两种情况的组合,但没有工作:

Case When (cust_shipmentdate_awb Is Null OR cust_shipmentdate_awb = '') AND 
(comp_shipdate_awb IS NULL OR comp_shipdate_awb = '') AND (apbg_bal_pay_amt != '0') Then 
'Pending' ELSE 'Completed' End AS overall_status 

2 个答案:

答案 0 :(得分:0)

我认为有两种解决方案: 1.使当前查询成为子查询,并在子查询的结果上添加另一个CASE语句。 (我看到的漂亮解决方案) 2.在现有案例中添加另一个WHEN。结合前两个WHEN子句中的子句来获得结果。 (可能是最优化的解决方案)

答案 1 :(得分:0)

这个怎么样?

(case when (cust_shipmentdate_awb Is Null OR cust_shipmentdate_awb = '')  and
           (comp_shipdate_awb IS NULL OR comp_shipdate_awb = '')
      then 'Not Completed'
      when apbg_bal_pay_amt = '0' Then 'Completed'
      else 'Not Completed'
 end) as overall_status