Handling Null in Greatest function in Oracle

后端 未结 9 1399
轮回少年
轮回少年 2021-02-14 05:36

I want to compare two dates from two columns and get the greatest and then compare against a date value.The two column can hold NULL values too.For example I want the below OUTP

9条回答
  •  梦如初夏
    2021-02-14 06:08

    If you have many columns to compare (more than 2 or 3), then handling all the various CASE combinations might get unwieldy. You could try (11g):

    with x as (
      select 1 as id, sysdate - 30 as col1, sysdate-50 as col2, sysdate-20 as col3,null as col4, sysdate-1 as col5 from dual
      union
      select 2 as id, sysdate - 10 as col1, sysdate-20 as col2, null as col3,null as col4, sysdate-35 as col5 from dual
      union
      select 3 as id, null as col1, null as col2, null as col3, null as col4, null as col5 from dual
    )
    select id, max(dates)
    from x
    UNPIVOT INCLUDE NULLS
    (dates FOR colname IN (col1,col2,col3,col4,col5))
    group by id
    

提交回复
热议问题