2016-11-06 36 views
1

返回null对于下面输入最大功能在Oracle

Employee_ID Date_Column 
100   12/12/2016 
100   15/12/2016 
200   19/12/2016 

我使用的查询

select employee_id,Max(Date_Column),Min(Date_Column) from Employee_ID where Date_Column is not null Group by Employee_ID 

检索最大和最小日期

但如果我的date_column有一个只有一个日期员工Max和Min函数都返回相同的日期。

但是我预期的输出是

employee_id Max(Date_Column) Min(Date_Column) 
100   15/12/2016  12/12/2016 
200   NULL   19/12/2016 

你的帮助是非常赞赏

回答

2

只有一个值,甲骨文代码是正确的。你可以用条件逻辑做你想做的事:

select employee_id, 
     (case when max(date_column) <> min(date_column) then Max(Date_Column) end), 
     Min(Date_Column) 
from Employee_ID 
where Date_Column is not null 
Group by Employee_ID; 
+0

非常感谢@Gordon Linoff :) –