2016-02-19 78 views
0

列角色_id has13和22作爲id,需要從列cmat_customer_id中獲取不匹配的ID。 請參閱下面的參考數據」查找不匹配的數據

SYSTEM_SERIAL_NUMBER ROLE_ID TOTAL_ROLE_ID CMAT_CUSTOMER_ID CMAT_SITE_ID 
200000301498 13 1 6082581 -999999 
200000304907 13 1 5018171 -999999 
200000273689 13 1 5008520 -999999 
200000280436 13 1 5008520 -999999 
451507000097 22 1 5013865 -999999 
451448000036 13 1 5008621 5008622 
451507000148 22 1 5013459 -999999 
FB6500N120181 13 1 5002239 6019184 
FB6500N120181 22 1 5002239 6019184 
451507000097 22 1 5013865 5013867 
451507000097 13 1 5013865 -999999 

預計resulet

SYSTEM_SERIAL_NUMBER ROLE_ID count_ROLE_ID CMAT_CUSTOMER_ID CMAT_SITE_ID 
200000301498 13 1 6082581 -999999 
200000301498 22 1 5018171 -999999 
200000304907 13 1 5018172 -999999 
200000304907 22 1 5008520 -999999 
451507000148 13 1 5008512 -999999 

我們不希望下面的結果集

SYSTEM_SERIAL_NUMBER ROLE_ID TOTAL_ROLE_ID CMAT_CUSTOMER_ID CMAT_SITE_ID 
451449000141 13 1 6097038 9409647 
451449000141 22 1 6097038 9409647 

回答

1

一種方法是使用not exists

select t.* 
from t 
where (role_id = 13 and 
     not exists (select 1 from t t2 where t2.cmat_customer_id = t.cmat_customer_id and t2.role_id = 22) 
    ) or 
     (role_id = 22 and 
     not exists (select 1 from t t2 where t2.cmat_customer_id = t.cmat_customer_id and t2.role_id = 13) 
    ); 
+0

謝謝Gordon,在表中TOTAL_ROLE_ID列實際上是我用來計算role_id(13和22)不匹配總數的role_id的計數。在上面的查詢中,我得到了結果集,但無法確定每個role_id不匹配的總數。我正在嘗試計算不匹配的列,其中列cid_customer_id的Role ID = 22與列cmat_customer_id ....的列ID = 13不同。我需要這些字段system_serial_number,role_id,cmat_customer_id,cmat_site_id以及count – DheerajJain