Handling Null in Greatest function in Oracle

后端 未结 9 1386
轮回少年
轮回少年 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:01

    use NVL to solve this however complicity will be increased based of number of compared columns :

    select A.*, *greatest("COL A", "COL B") "DIRECT COMPARE"*, **greatest(nvl("COL A", "COL B"), nvl("COL B", "COL A")) "NVL COMPARE"**
    from (
    SELECT NULL "COL A", SYSDATE "COL B", SYSDATE "NEEDED RESULT" FROM DUAL UNION
    SELECT SYSDATE - 180 , NULL  , SYSDATE - 180 FROM DUAL UNION
    SELECT SYSDATE - 180 , SYSDATE , SYSDATE FROM DUAL ) A;
    
    0 讨论(0)
  • 2021-02-14 06:02

    Another version using a case expression to handle the null values:

    select cola, colb, 
      case when cola is null and colb is null then null
        when cola is null then colb
        when colb is null then cola
        else greatest(cola, colb)
      end as output
    from <table>;
    
    COLA       COLB       OUTPUT   
    ---------- ---------- ----------
    
    09/21/2013 01/02/2012 09/21/2013 
               01/03/2013 01/03/2013 
    01/03/2013            01/03/2013 
    
    0 讨论(0)
  • 2021-02-14 06:05

    You could remove the possibility of any of the columns returning NULL by using the NVL function. Substitute any NULL values with a date that is earlier than any date that is likely to occur in your tables.

    SELECT GREATEST(NVL(A,TO_DATE('01/01/1800','MM/DD/YYYY')), 
                    NVL(B,TO_DATE('01/01/1800','MM/DD/YYYY'))) AS OUTPUT
    FROM ...
    

    The GREATEST function will then return the most recent date (maximum date) from the list of supplied dates without inadvertently returning NULL if one or more of the columns contains NULL.

    0 讨论(0)
  • 2021-02-14 06:06

    Something like

    SELECT CASE WHEN ColA is NULL and ColB is NULL then NULL
    WHEN coalesce(ColA, '01/01/1753')>coalesce(ColB, '01/01/1753') then ColA
    ELSE ColB END as Output
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-02-14 06:18

    I tried this..found after googling

    WITH ABC AS ( SELECT NULL AS col1 , NULL AS col2 FROM dual UNION
     SELECT NULL , DATE'2013-08-12' FROM dual UNION
      SELECT  DATE'2013-08-12' , NULL FROM dual UNION
       SELECT  DATE'2013-08-12',  DATE'2013-09-12' FROM dual)
    
       SELECT col1, col2 , substr(greatest('x'||col1,'x'||col2),2)
          FROM ABC;
    
    0 讨论(0)
提交回复
热议问题