MySQL:结合联盟和前3名潜在客户

时间:2017-07-25 19:50:31

标签: mysql sql union

我正在尝试获取每个广告系列类型以及每个广告系列类型中参与人数最多(前3名)的三个主要名称。它应该看起来像这样:

+-------+-----------+
| Code  |  LeadName |     
+-------+-----------+
| WP    | LEAD A    |    
| WP    | LEAD B    |   
| WP    | LEAD C    |   
| CF    | LEAD B    |    
| CF    | LEAD C    |   
| CF    | LEAD A    |  
| TL    | LEAD B    |    
| TL    | LEAD A    |   
| TL    | LEAD C    |  


Campaign Table: CampaignID, CampaignName, CampaignStartDate, Code
Campaign Members Table:CampaignID, LeadID, CampaignMemberStatus, CampaignMemberSignUpDate
Leads Table: LeadID, LeadName, AccountID

CampaignID链接广告系列 - >会员,LeadID链接会员 - >信息 我正在努力想要如何结合并提供前三名的LeadNames。

1 个答案:

答案 0 :(得分:-2)

SELECT TOP 3 ABC.Code,ABC.LeadName 
FROM 
(
SELECT A.Code,C.LeadName,COUNT(*) as total 
FROM Campaign as A
LEFT JOIN [Campaign Members] as B
ON B.CampaignID = A.CampaignID
LEFT JOIN Leads as C
ON C.LeadID = B.LeadID
GROUP BY A.Code, C.LeadName
ORDER BY COUNT(*) 
) as ABC