0

サンプルクエリ

SELECT count(A.ID) 
FROM `TABLE_A` A LEFT JOIN `TABLE_B` B ON (A.ID=B.ID) 
WHERE .....

私は欲しい:

表Bの複数のレコードに一致する場合でも、A.id 1つだけと見なす必要があります。

実際のクエリ

SELECT CampaignName,fsbcorponline.leads360campaigns.CostPerLead, 
count(fsbcorponline.leads360leads.id) leadCount,
count(CASE WHEN fsbcorponline.leads360leads.leadStatus like '%Contact Attempt%' OR                                
leadStatus like '%Call Back/Follow-up%' OR                                 
leadStatus like '%Working%' OR                                 
leadStatus like '%Credit Pulled%' OR                                 
leadStatus like '%Selected other Lender%' OR                                 
leadStatus like '%Pre-Approved%' OR                                 
leadStatus like '%Application Taken/Sent%' OR                                 
leadStatus like '%Application Received%' OR                                 
leadStatus like '%Submitted%' OR                                
leadStatus like '%Funded%' OR                                 
leadStatus like '%Future Opportunity%' OR                                 
leadStatus like '%Manager Review%' THEN 1 END ) leadContacted,         
COUNT(CASE WHEN fsbcorponline.leads360leads.lead_id IS NOT NULL THEN 1 END ) leadsToEncompass ,          
COUNT(CASE WHEN fsbcorponline.leads_to_loan.loan_closeDate IS NOT NULL AND loan_closeDate !='1900-01-01 00:00:00' THEN 1 END ) leadsToEncompassClose,         
COUNT(CASE WHEN fsbcorponline.leads_to_loan.loan_FundedDate IS NOT NULL AND loan_FundedDate !='1900-01-01 00:00:00' THEN 1 END ) leadsToEncompassFunded  
FROM (fsbcorponline.leads360leads LEFT JOIN fsbcorponline.leads360campaigns ON fsbcorponline.leads360leads.CampaignId=fsbcorponline.leads360campaigns.CampaignId)  
LEFT JOIN fsbcorponline.leads_to_loan ON fsbcorponline.leads360leads.id =lead_id  
Where 1=1 AND dateAdded between '11/24/2012 08:59:00' and '12/24/2012 08:59:00'AND CampaignName like '%TFSB.com%'
4

2 に答える 2

2

これを試してみてdistinct countください、

SELECT count(distinct A.ID) 
FROM `TABLE_A` A JOIN `TABLE_B` B ON (A.ID=B.ID) 
WHERE .....

あなたは一致するレコードだけを心配しているので、単にそうするだけJOINで、必要はありませんLEFT JOIN

SELECT CampaignName,fsbcorponline.leads360campaigns.CostPerLead, 
count( distinct fsbcorponline.leads360leads.id) leadCount,
count(
CASE WHEN leadStatus like '%Contact Attempt%' OR                                
leadStatus like '%Call Back/Follow-up%' OR                                 
leadStatus like '%Working%' OR                                 
leadStatus like '%Credit Pulled%' OR                                 
leadStatus like '%Selected other Lender%' OR                                 
leadStatus like '%Pre-Approved%' OR                                 
leadStatus like '%Application Taken/Sent%' OR                                 
leadStatus like '%Application Received%' OR                                 
leadStatus like '%Submitted%' OR                                
leadStatus like '%Funded%' OR                                 
leadStatus like '%Future Opportunity%' OR                                 
leadStatus like '%Manager Review%' THEN 1 END ) leadContacted,         
COUNT
(CASE WHEN lead_id IS NOT NULL THEN 1 END ) leadsToEncompass ,          
COUNT(CASE WHEN loan_closeDate IS NOT NULL AND loan_closeDate !='1900-01-01 00:00:00' THEN 1 END ) leadsToEncompassClose,         
COUNT(CASE WHEN loan_FundedDate IS NOT NULL AND loan_FundedDate !='1900-01-01 00:00:00' THEN 1 END ) leadsToEncompassFunded  
FROM (fsbcorponline.leads360leads LEFT JOIN fsbcorponline.leads360campaigns ON fsbcorponline.leads360leads.CampaignId=fsbcorponline.leads360campaigns.CampaignId)  
LEFT JOIN fsbcorponline.leads_to_loan ON fsbcorponline.leads360leads.id =lead_id  
Where 1=1 AND dateAdded between '11/24/2012 08:59:00' and '12/24/2012 08:59:00'AND CampaignName like '%TFSB.com%'
于 2012-12-24T15:46:07.513 に答える
2

このGROUP BY目的のために句を使用できます。

SELECT COUNT(A.ID) 
FROM `TABLE_A` A LEFT JOIN `TABLE_B` B ON (A.ID=B.ID) 
WHERE .....
GROUP BY A.ID
于 2012-12-24T15:48:36.500 に答える