MySQL选择最大日期或空日期

我想选择最大日期或null但它显示错误消息.

Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=IS;, CASE WHEN MAX(DATE;CONCAT, DRIVER=3.64.114

原来的表是

Employee code           resignation date
001                     1/2/2013
001                     1/5/2014
001                     null
002                     10/10/2000

应该显示

EMPLOYEE_CODE           RESIGNATION_DATE
001                     null
002                     10/10/2000

这是我的疑问

SELECT EMPLOYEE_CODE, 
       CASE 
           WHEN MAX(RESIGNATION_DATE IS NULL)= 0 
           THEN MAX(RESIGNATION_DATE) 
       END AS DATE
FROM MT_EMPLOYEE_CONTRACT
GROUP BY EMPLOYEE_CODE;

任何人都可以找出问题所在吗?
谢谢

最佳答案

Can Anyone find out what is the problem?

在这种情况下:

MAX(RESIGNATION_DATE IS NULL)= 0

你正在比较MAX(NULL)= 0,因为RESIGNATION_DATE是NULL

您的查询应该是:

select emp_code, res_date /* first select null emp_code*/
From Table_name
where res_date IS NULL group by emp_code
UNION 
select emp_code, MAX(res_date ) /* select max from non - null emp_code*/
From Table_name
where emp_code NOT IN 
     (select emp_code From Table_name 
      where res_date IS NULL) 
group by emp_code

编辑

select DISTINCT emp_code, res_date /* first select null emp_code*/
From Table_name
where res_date IS NULL 
UNION 
select emp_code, MAX(res_date ) /* select max from non - null emp_code*/
From Table_name
where emp_code NOT IN 
     (select emp_code From Table_name 
      where res_date IS NULL) 
group by emp_code 
点赞