2017-07-28 46 views
-1

我有一個要求,如果我得到的數據與特定列不是null,那麼我需要獲取所有相應的記錄source_type,否則我需要獲取所有記錄基於其他欄目。讓我們以下面記錄確定哪裏條件基於列值

enter image description here

的例子在上述情況下,分組基於列GRP完成。在該特定組中,如果source_type列對於任何記錄都不是null,我們需要從該組中獲取SOURCE列中具有相同值的所有記錄,其中source_type不是null。在這種情況下,期望的輸出是

enter image description here

但在組內情況下,如果source_typenull所有記錄,我們需要獲取從該組具有在相同值的所有記錄source的列,其中MATCH_TYPE='MP'。在這種情況下,期望的輸出是

enter image description here

+0

請填寫剩下的第三張截圖。 – nop77svk

回答

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$ 
; 

...萬一我正確理解你的用例。

+0

非常感謝您的查詢。我很抱歉地說,我沒有按預期得到結果。 – GIN

+0

@GIN,那是因爲你沒有正確而徹底地說出你的問題。 – nop77svk