[英]CASE Statement with conditions
There are two fields in my table cust_shipmentdate_awb and comp_shipdate_awb. 我的表cust_shipmentdate_awb和comp_shipdate_awb中有两个字段。 Iam trying to write a CASE Statement which should check both fields cust_shipmentdate_awb and comp_shipdate_awb is null or empty.
我试图编写一个CASE语句,该语句应该检查两个字段cust_shipmentdate_awb和comp_shipdate_awb为null或为空。 If both are Null then show the result as 'Pending'.
如果两者均为Null,则将结果显示为“待处理”。 If any one field is not empty, then it should show as 'Completed' AND if both fields are entered also it should as 'Completed'.
如果任何一个字段都不为空,则应显示为“已完成”,如果同时输入了两个字段,也应显示为“已完成”。 I have tried the below CASE statement.
我已经尝试了下面的CASE语句。 But its showing pending when both the fields data is entered.
但是,当同时输入两个字段数据时,它显示为未决。
CASE
When cust_shipmentdate_awb Is Null Or cust_shipmentdate_awb = '' Then 'Pending'
Else 'Completed' End AS shipment_status
Use a subquery. 使用子查询。
SELECT CASE WHEN shipment_date Is Null Or shipment_date = ''
THEN 'Pending'
Else 'Completed'
End AS shipment_status
FROM (
SELECT CASE
When commercial_logi_freight.cust_shipmentdate_awb Is Null Or
commercial_logi_freight.cust_shipmentdate_awb = ''
Then commercial_logi_freight.comp_shipdate_awb
Else commercial_logi_freight.cust_shipmentdate_awb
End AS shipment_date
FROM ...) AS x
You need to put your query inside a subquery then refer to that shipment_date
field outside of that subquery like below: 您需要将查询放入子查询中,然后在该子查询之外引用该
shipment_date
字段,如下所示:
SELECT
subQuery.*,
Case When shipment_date Is Null Or shipment_date = '' Then 'Pending' Else 'Completed' End AS shipment_stat
FROM
(
#YOUR QUERY GOES HERE.....
SELECT
CASE
When commercial_logi_freight.cust_shipmentdate_awb Is Null Or
commercial_logi_freight.cust_shipmentdate_awb = '' Then
commercial_logi_freight.comp_shipdate_awb
Else commercial_logi_freight.cust_shipmentdate_awb End AS shipment_date
FROM your_table
) AS subQuery
You can only use column aliases in GROUP BY, ORDER BY, or HAVING clauses.
您只能在GROUP BY,ORDER BY或HAVING子句中使用列别名。
This should work 这应该工作
CASE
When commercial_logi_freight.cust_shipmentdate_awb Is Null Or commercial_logi_freight.cust_shipmentdate_awb = '' Then commercial_logi_freight.comp_shipdate_awb
Else commercial_logi_freight.cust_shipmentdate_awb End AS shipment_date,
CASE
When commercial_logi_freight.cust_shipmentdate_awb Is Null Or commercial_logi_freight.cust_shipmentdate_awb = '' Then 'Pending'
Else 'Completed' End AS shipment_status
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.