2015-10-21 123 views
2

我想将以下两个查询合并为一个子查询中的一个查询。 首先查询:将两个查询合并到一个子查询中

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

+1

在哪里可以找到重复;只在p/n记录或所有记录中?你称之为重复的东西;出现多次的match_ref或多次出现的match_ref/wbe对? –

回答

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

+0

这很好。如果想想,我必须改变自己的方式。 –

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