两个查询到一个子查询中

时间:2015-10-21 19:18:30

标签: sql ms-access ms-access-2010

我想将以下两个查询合并到一个查询中,然后查询子查询。 第一个查询:

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

您可以大大简化您想要做的事情:

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”是否一起发生(根据您的查询示例)。