您是否尝试显示 D1、D2、D3、D4、D5 中的第一个非空列?如果是这样,您最好使用COALESCE(D1, D2, D3, D4, D5)。
你的case语句不会像你期望的那样工作,因为
a) "==" 在 Oracle 中不是有效的语法
b) 比较 <something> = null(以及相应的 <something> != null)总是返回 null - 即。它既不是真的也不是假的。相反,您应该检查:<something> is null(反之:<something> is not null)。
c) 您的逻辑不完整 - 您正在检查如果 D1 到 D5 为空该怎么办,而不是如果 D1(或 D2 或...)不为空会发生什么。
假设您想要第一个非空值,这应该让您了解如何使用合并和案例逻辑来做到这一点:
with sample_data as (select 1 col1, null col2, null col3 from dual union all
select null col1, 2 col2, null col3 from dual union all
select null col1, null col2, 3 col3 from dual union all
select null col1, 4 col2, 5 col3 from dual union all
select 6 col1, 7 col2, 8 col3 from dual union all
select 9 col1, null col2, 10 col3 from dual)
select col1,
col2,
col3,
coalesce(col1, col2, col3) first_non_null_coalesce,
case when col1 is not null then col1
when col2 is not null then col2
when col3 is not null then col3
end first_non_null_case_logic
from sample_data;
COL1 COL2 COL3 FIRST_NON_NULL_COALESCE FIRST_NON_NULL_CASE_LOGIC
---------- ---------- ---------- ----------------------- -------------------------
1 1 1
2 2 2
3 3 3
4 5 4 4
6 7 8 6 6
9 10 9 9