如果我理解的很好,這可能是一種方法。 假設你已經擁有你[R查詢得到的結果,你可以用這種方式:
with yourResult(server_id, server_name, explicit_perm, rank, total) as (
select 93, 'AVIZNER', 1, 1, 10 from dual union all
select 93, 'AVIZNER', 0, 6, 10 from dual union all
select 11, 'XXXXXXX', 1, 1, 10 from dual union all
select 22, 'YYYYYYY', 0, 1, 10 from dual union all
select 11, 'ZZZZZZZ', 1, 1, 11 from dual union all
select 11, 'ZZZZZZZ', 1, 2, 22 from dual union all
select 11, 'ZZZZZZZ', 0, 1, 10 from dual
)
select server_id, server_name, explicit_perm, rank, total
from (
select server_id, server_name, explicit_perm, rank, total,
count (case when explicit_perm = 1 then 1 end) over (partition by server_id) as count_perm_1
from yourResult
)
where not (explicit_perm = 0 and count_perm_1 > 0)
這種計算行數與explicit_perm = 1
爲每server_id
,然後排除所有explicit_perm = 0
和至少一行行與explicit_perm = 1
存在相同的server_id
。
與我的樣本數據,結果:
SERVER_ID SERVER_ EXPLICIT_PERM RANK TOTAL
---------- ------- ------------- ---------- ----------
11 ZZZZZZZ 1 2 22
11 ZZZZZZZ 1 1 11
11 XXXXXXX 1 1 10
22 YYYYYYY 0 1 10
93 AVIZNER 1 1 10
如何簡單地使用外側的位置,我的意思是脫離了union的括號? –
我需要放棄EXPLICIT_PERM = 0的行,只有在服務器ID出現兩次的情況下,我該如何檢測? –
爲什麼你想用PL/SQL(一種編程語言)而不是SQL來解決這個問題? –