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

oracle - consultando valores NULL na consulta unpivot


Use UNPIVOT INCLUDE NULLS :

SQL Fiddle

Configuração do esquema Oracle 11g R2 :
CREATE TABLE test ( id, a, b, c, d ) AS
SELECT 1, 1, 2, 3, 4 FROM DUAL UNION ALL
SELECT 2, 1, NULL, 3, NULL FROM DUAL;

Consulta 1 :
SELECT *
FROM   test
UNPIVOT INCLUDE NULLS ( value FOR name IN ( a, b, c, d ) )

Resultados :
| ID | NAME |  VALUE |
|----|------|--------|
|  1 |    A |      1 |
|  1 |    B |      2 |
|  1 |    C |      3 |
|  1 |    D |      4 |
|  2 |    A |      1 |
|  2 |    B | (null) |
|  2 |    C |      3 |
|  2 |    D | (null) |