oracle - 在unpivot查询中查询NULL值

时间:2017-10-16 04:59:15

标签: oracle null unpivot

我想从oracle DB中获取列值为NULL的记录。我也在我的查询中使用unpivot。由于NULL关键字,某些unpivot值未被选中。在使用unpivot时,您可以帮助我了解如何获取相同行。

修改

SELECT a.emp_id, a.emp_dept, b.emp_location
  FROM employee a,
       location b UNPIVOT (emp_id
                  FOR dummy_id
                  IN (emp_id AS 'EMP_ID', last_date AS 'LAST_DATE'))
 WHERE emp_id = 123 AND b.emp_loc_id = 'india' AND b.location IS NULL;

1 个答案:

答案 0 :(得分:2)

使用UNPIVOT INCLUDE NULLS

SQL Fiddle

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;

查询1

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

<强> Results

| 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) |