根据列值确定条件的位置

时间:2017-07-28 07:10:06

标签: oracle

我要求如果我获取特定列的数据而不是null,那么我需要获取与source_type对应的所有记录,否则我需要获取所有记录在其他专栏上。我们以下面的记录为例

enter image description here

在上述情况下,分组是基于列GRP完成的。在该特定组中,如果任何记录的source_type列不是null,我们需要从该组中获取SOURCE列中具有相同值的所有记录,其中source_type不是null。在这种情况下,预期输出是

enter image description here

但是如果组中的source_type列对于所有记录都是null,我们需要从source中获取该组中具有相同值的所有记录MATCH_TYPE='MP'的列。在这种情况下,预期输出为

enter image description here

1 个答案:

答案 0 :(得分:0)

对分析函数来说看起来不错......

with xyz as (
    select X.*,
        count(X.source_type) over (partition by X.grp, X.source) as non_null_source_type#,
        first_value(case when X.match_type = 'MP' then X.source end) over (partition by X.grp) as source_from_mp$
    from table X
)
select *
from xyz
where non_null_source_type# > 0
    or non_null_source_type# = 0
        and source = source_from_mp$
;

...以防我正确理解您的用例。

相关问题