2016-02-29 76 views
-2

我有3個表格。第一個表 'Status_Mapping' 有以下列從3表中獲取無與倫比的記錄

Status_original Status_Site 
accepted   Call Verified 
duplicate  Duplicate Leads 
dq    DQ 

'Lead_transaction' 有列:

Lead_transaction_id Rate Status 
    11     0.01 accepted 
    12     0.02 accepted 
    13     0.01 newstatus 

'Lead_Instance' 表:

Lead_Instance_id Lead_transaction_id product_id affiliate_id 
1     11     6   10 
2     12     7   11 
3     13     6   10 

我想要做的就是在計數(lead_instance_id)和狀態總和(比率),這些狀態在status_mapping表中不存在,並且應顯示狀態爲「其他」,其中product_id = 6且affiliate_id = 10我的最終結果應該如

Total    Sum Status 
1     0.01 Other 
+1

[查找不匹配的記錄與SQL]的可能的複製(http://stackoverflow.com/questions/508509/finding-unmatched-records-with -SQL) –

回答

-1

你可以用這個查詢開始:

select count(distinct a.Lead_Instance_id), sum(b.Rate) 
from 
Lead_Instance as a 
inner join 
Lead_transaction as b 
on (a.Lead_transaction_id = b.Lead_transaction_id) 
where 
b.Status not in (select distinct Status_original from Status_Mapping) 
and a.product_id = 6 
and a.affiliate_id = 10