两个查询合并为一个子查询

Two queries into one subquery

我想将以下两个查询合并为一个查询和一个子查询。 第一个查询:

Select Match_ref, WBE Into Match_ref_Confilct
From RAW_MWBE
where WBE="p" or WBE="n"
group by Match_ref, WBE

第二个查询:

Select Match_ref, count(Match_ref)
from Match_ref_conflict

这样做的目的是最终得到 match_refs 的列表,这些列表出现不止一次,因此存在信息冲突。 我尝试了这个但没有成功:

Select match_Ref, count(match_ref)
From RAW_MWBE
where Exists( Select match_ref, WBE
from RAW_MWBE
where WBE like "P" or WBE like "N")
group by match_ref, WBE
having Count(Match_ref)>1

访问SQL

Select Match_ref, count(*) as cnt
From RAW_MWBE
where WBE="p" or WBE="n"
group by Match_ref
having count(*) > 1

你可以大大简化你想做的事情:

Select Match_ref, count(Match_ref)
from RAW_MWBE
where WBE in ("p", "n")
group by Match_Ref
having min(WBE) <> max(WBE);

这不使用 count(),因为您似乎关心 "p" 和 "n" 是否同时出现(根据您的查询示例)。