另一種選擇。實際上,這最終與@ trincot的解決方案非常相似,我只是將邏輯從計算邏輯中分離出來。如果你的邏輯在未來變得更加複雜,這可能會更靈活一些。
with
inputs (system, status) as (
select 'PRE1-SYS1', 'SUCCESS' from dual union all
select 'PRE1-SYS2', 'SUCCESS' from dual union all
select 'PRE2-SYS1', 'RUNNING' from dual union all
select 'PRE2-SYS2', 'SUCCESS' from dual union all
select 'PRE3-SYS1', 'SUCCESS' from dual union all
select 'PRE3-SYS2', '' from dual
),
/* The cnts CTE counts how many rows relate to a SYSTEM,
how many of those are SUCCESS, and how many are NULL.
*/
cnts(system, num_rows, num_success, num_null) as (
select substr(system,1,instr(system, '-')-1) system,
count(*),
sum(case when status = 'SUCCESS' then 1 else 0 end),
sum(case when status is null then 1 else 0 end)
from inputs
group by substr(system,1,instr(system, '-')-1)
)
/* Using the counts from the CTE, we can implement whatever logic we
want
*/
select system,
(case when num_rows = num_success then 'SUCCESS'
when num_rows = num_null then 'PENDING'
else 'RUNNING'
end) status
from cnts
要求仍不清楚。 「或者,如果並且只有,那麼PENDING」在英語中沒有意義,你想說什麼?你是說如果至少有一個'null',那麼狀態是PENDING?如果你有一行RUNNING和另一個'null' - 是PENDING還是RUNNING?請注意,LISTAGG與此無關。 – mathguy
應該說空白,但我用尖括號,所以它HTML'd我。定影。 – dsbalaban
感謝您的編輯。 PRE3顯示SUCCESS和'null',但結果是PENDING,但用文字說「如果只有NULL」,則爲「待處理」。這是什麼? – mathguy