我试图为下面的表结构写一个CASE语句:
========================================
tender_cust_shortcode | open_auth_date
========================================
HAL Hydera | 24-01-2016
DRDL | NULL
HAL ARDC | 10-02-2016
DLRL | NULL
ISAC | NULL
========================================
我的CASE声明如下:
CASE WHEN (tender_cust_shortcode = 'HAL Hydera' OR 'HAL ARDC') THEN 'N/A'
WHEN (tender_cust_shortcode != 'HAL Hydera' OR 'HAL ARDC') AND open_auth_date IS NULL
THEN 'Pending'
ELSE 'Completed' END AS `Open Auth Status`
标准是
1)if(tender_cust_shortcode =' HAL Hydera' OR' HAL ARDC')然后它应该导致' N / A'
2)if(tender_cust_shortcode!=' HAL Hydera' OR' HAL ARDC')和open_auth_date IS NULL然后它应该导致'等待'
3)Else'已完成'。
我没有得到所需的输出。可能是AND或OR条件问题。请有人帮我这个吗?
答案 0 :(得分:1)
尝试使用IN
,不确定这是否有效,但请尝试一下:
CASE
WHEN tender_cust_shortcode IN ('HAL Hydera', 'HAL ARDC') THEN 'N/A'
WHEN tender_cust_shortcode NOT IN ('HAL Hydera', 'HAL ARDC') AND open_auth_date IS NULL THEN 'Pending'
ELSE 'Completed'
END AS `Open Auth Status`
这是SQLFiddle Demo,它有效。: - )
答案 1 :(得分:0)
您必须重复列名称,并且必须通过AND
连接或者不相等条件:
CASE WHEN (tender_cust_shortcode = 'HAL Hydera' OR tender_cust_shortcode = 'HAL ARDC') THEN 'N/A'
WHEN (tender_cust_shortcode != 'HAL Hydera' AND tender_cust_shortcode != 'HAL ARDC') AND open_auth_date IS NULL
THEN 'Pending'
ELSE 'Completed' END AS `Open Auth Status`
请参阅SQLFiddle
答案 2 :(得分:-1)
尝试这样的查询:
SELECT CASE WHEN (tender_cust_shortcode = 'HAL Hydera' OR 'HAL ARDC') THEN 'N/A'
WHEN ((tender_cust_shortcode != 'HAL Hydera' OR 'HAL ARDC') AND (open_auth_date IS NULL)) THEN 'Pending'
ELSE 'Completed' END FROM `test`