2015-10-21 162 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