简体   繁体   English

带条件的CASE语句MYSQL

[英]CASE Statement MYSQL with Condition

I got 2 CASE statements: First CASE statement is as follows: 我有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

The Second CASE statement is as follows: 第二个CASE语句如下:

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

Iam looking to write one more CASE statement named OVERALL_Status. 我正在寻找写另一条名为OVERALL_Status的CASE语句。 That is basically a combination of both this CASES (shipment_status and payment_status), which means if Shipment status is 'Shipped' AND Payment_Status is 'Received' then Overall_status is 'Completed' else 'Not Completed'. 这基本上是这两种情况(shipment_status和payment_status)的组合,这意味着如果“装运状态”为“已发货”并且“ Payment_Status”为“已接收”,则“总体状态”为“已完成”,否则为“未完成”。 Can anyone please help me on this. 谁能帮我这个忙。 I am really struck here. 我真的很感动 I tried the combination of both the CASES, but not working: 我尝试了两种情况的组合,但是没有用:

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 

There is two solutions as I see it: 1. You make the current query a subquery and add another CASE statement on the result of the subquery. 我看到有两种解决方案:1.您将当前查询设为子查询,并在子查询的结果上添加另一个CASE语句。 (the pretty solution as I see it) 2. You add another WHEN to the existing case. (如我所见,这是一个不错的解决方案)2.您向现有案例中添加另一个WHEN。 Combining the clauses in the first two WHEN clauses to get the result. 组合前两个WHEN子句中的子句以获得结果。 (probably the most optimized solution) (可能是最优化的解决方案)

How about this? 这个怎么样?

(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

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

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