Oracle
 sql >> Base de Dados >  >> RDS >> Oracle

Oracle SQL - max() com valores NULL


max(end_dt) keep (dense_rank first order by end_dt desc nulls first)

atualização:

SQL Fiddle

Configuração do esquema Oracle 11g R2 :
CREATE TABLE t
    (val int, s date, e date)
;

INSERT ALL 
    INTO t (val, s, e)
         VALUES (1, sysdate-3, sysdate-2)
    INTO t (val, s, e)
         VALUES (1, sysdate-2, sysdate-1)
    INTO t (val, s, e)
         VALUES (1, sysdate-1, null)
    INTO t (val, s, e)
         VALUES (2, sysdate-1, sysdate-.5)
    INTO t (val, s, e)
         VALUES (2, sysdate-.5, sysdate-.25)
SELECT * FROM dual
;

Consulta 1 :
select val, min(s), max(e) keep (dense_rank first order by e desc nulls first)
from t group by val

Resultados :
| VAL |                          MIN(S) | MAX(E)KEEP(DENSE_RANKFIRSTORDERBYEDESCNULLSFIRST) |
---------------------------------------------------------------------------------------------
|   1 | November, 13 2012 14:15:46+0000 |                                            (null) |
|   2 | November, 15 2012 14:15:46+0000 |                   November, 16 2012 08:15:46+0000 |