2016-11-06 37 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 :) –