我正在尝试使用SQL从以下数据集中检索记录。对于POKey和PAKey的每种组合,我都试图查找是否存在RCode 9和RCode 10,而缺少RCode 8。
我已附上示例数据,请帮助我了解如何操作。
答案 0 :(得分:0)
这是您想要的吗?
select PoKey, PaKey, 'Missing 8'
from t
group by PoKey, PaKey
having sum(case when rcode = 9 then 1 else 0 end) > 0 and
sum(case when rcode = 10 then 1 else 0 end) > 0 and
sum(case when rcode = 8 then 1 else 0 end) = 0;
答案 1 :(得分:0)
我会使用NOT EXISTS
:
select distinct t.PoKey, t.PaKey, 'Missing 8' as Rcode
from table t
where RCode in (9, 10) and
not exists (select 1
from table t1
where t1.PoKey = t.PoKey and
t1.PaKey = t.PaKey and
t1.Rcode = 8
);