确定在 ORACLE COALESCE 中使用了什么源
Identify what source was used in ORACLE COALESCE
如何确定 COALESCE 返回的数据来源?
我想要这样的东西:
SELECT
COALESCE(SOURCE1, SOURCE2) SOURCE,
SOURCE_LOACTION
FROM
DATATABLE1 D1, DATATABLE2 D2
我在结果中有一列显示数据的来源
答案:
像往常一样,我的问题措辞不够好,所以我不得不从每个人提供的内容中弥补我需要的内容:
-- I was using COALESCE on more than one field
COALESCE(D1.FIELD1, D2.FIELD1) FIELD1,
COALESCE(D1.FIELD2, D2.FIELD2) FIELD2,
COALESCE(D1.FIELD3, D2.FIELD3) FIELD3,
CASE WHEN
D1.FIELD1 IS NULL AND
D1.FIELD2 IS NULL AND
D1.FIELD3 IS NULL AND
THEN 'SOURCE2' ELSE 'SOURCE1' END AS DATASOURCE
感谢您的帮助:)
我会这样做:
with sample_data as (select 1 id, 'a' source1, null source2 from dual union all
select 2 id, null source1, null source2 from dual union all
select 3 id, 'a' source1, 'b' source2 from dual union all
select 4 id, null source1, 'b' source2 from dual)
select id,
coalesce(source1, source2) source,
case when source1 is not null then 'source1'
else 'source2'
end source_location1,
case when source1 is not null then 'source1'
when source2 is not null then 'source2'
end source_location2
from sample_data;
ID SOURCE SOURCE_LOCATION1 SOURCE_LOCATION2
---------- ------ ---------------- ----------------
1 a source1 source1
2 source2
3 a source1 source1
4 b source2 source2
N.B。当 source1 和 source2 都为空时,我提供了两种处理情况的方法;您选择哪一个取决于当两个来源都为 null 时您想要显示的内容 - null 或合并列表中提到的最后一个来源。
SELECT
COALESCE(SOURCE1, SOURCE2) SOURCE,
,COALESCE(nvl2(source1,'source1',null), nvl2(source2,'source2',null)),
FROM DATATABLE1 D1, DATATABLE2 D2
如何确定 COALESCE 返回的数据来源?
我想要这样的东西:
SELECT
COALESCE(SOURCE1, SOURCE2) SOURCE,
SOURCE_LOACTION
FROM
DATATABLE1 D1, DATATABLE2 D2
我在结果中有一列显示数据的来源
答案:
像往常一样,我的问题措辞不够好,所以我不得不从每个人提供的内容中弥补我需要的内容:
-- I was using COALESCE on more than one field
COALESCE(D1.FIELD1, D2.FIELD1) FIELD1,
COALESCE(D1.FIELD2, D2.FIELD2) FIELD2,
COALESCE(D1.FIELD3, D2.FIELD3) FIELD3,
CASE WHEN
D1.FIELD1 IS NULL AND
D1.FIELD2 IS NULL AND
D1.FIELD3 IS NULL AND
THEN 'SOURCE2' ELSE 'SOURCE1' END AS DATASOURCE
感谢您的帮助:)
我会这样做:
with sample_data as (select 1 id, 'a' source1, null source2 from dual union all
select 2 id, null source1, null source2 from dual union all
select 3 id, 'a' source1, 'b' source2 from dual union all
select 4 id, null source1, 'b' source2 from dual)
select id,
coalesce(source1, source2) source,
case when source1 is not null then 'source1'
else 'source2'
end source_location1,
case when source1 is not null then 'source1'
when source2 is not null then 'source2'
end source_location2
from sample_data;
ID SOURCE SOURCE_LOCATION1 SOURCE_LOCATION2
---------- ------ ---------------- ----------------
1 a source1 source1
2 source2
3 a source1 source1
4 b source2 source2
N.B。当 source1 和 source2 都为空时,我提供了两种处理情况的方法;您选择哪一个取决于当两个来源都为 null 时您想要显示的内容 - null 或合并列表中提到的最后一个来源。
SELECT
COALESCE(SOURCE1, SOURCE2) SOURCE,
,COALESCE(nvl2(source1,'source1',null), nvl2(source2,'source2',null)),
FROM DATATABLE1 D1, DATATABLE2 D2