0

示例查询

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

我需要:

即使A.id 匹配表 B 中的多条记录,也应将其视为仅一条。

实际查询

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 回答