user3559600
user3559600

Reputation: 147

MySQL select max date or null date

I would to select the max date or null but it shows a error message.

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

The original table is

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

should be show

EMPLOYEE_CODE           RESIGNATION_DATE
001                     null
002                     10/10/2000

this is my query

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? Thank you

Upvotes: 4

Views: 1036

Answers (2)

NoobEditor
NoobEditor

Reputation: 15891

Can Anyone find out what is the problem?

In this condition :

MAX(RESIGNATION_DATE IS NULL)= 0

you are comparing MAX(NULL)=0 as RESIGNATION_DATE IS NULL

Your query should be :

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

EDIT

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 

Upvotes: 3

Abdou Tahiri
Abdou Tahiri

Reputation: 4408

try to use a combination of NULLIF and IFNULL functions .

SELECT EMPLOYEE_CODE, NULLIF(MAX(IFNULL(RESIGNATION_DATE,0)),0)
FROM MT_EMPLOYEE_CONTRACT
GROUP BY EMPLOYEE_CODE;

Upvotes: 0

Related Questions