【问题标题】:Max function to return null in Oracle在 Oracle 中返回 null 的 Max 函数
【发布时间】:2017-03-20 05:52:00
【问题描述】:

以下输入

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

非常感谢您的帮助

【问题讨论】:

    标签: sql oracle max min


    【解决方案1】:

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

    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;
    

    【讨论】:

    • 非常感谢@Gordon Linoff :)
    猜你喜欢
    • 2016-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-03
    • 2021-06-06
    • 1970-01-01
    相关资源
    最近更新 更多