MYSQL查询以查找具有指定列分组的所有重复记录

时间:2016-04-27 08:42:50

标签: mysql

我有一张表格,其中包含为电子邮件广告系列发送的所有电子邮件:

ID | campaign_id | date_sent | date_delivered | email | status

Status包含投放信息,DELIVEREDUNDELIVERED。我想查找过去五个广告系列中状态为UNDELIVERED的所有电子邮件地址。

例如,email@address.com过去没问题且收到了所有广告系列,但现在电子邮件地址不存在,最后5个广告系列状态为UNDELIVERED

到目前为止,我正在做:

SELECT *, count(id) as occ FROM message_details WHERE status='FAILED' GROUP BY email

但这表明每个地址都没有送达,而不是最后5个。

3 个答案:

答案 0 :(得分:0)

阅读起来可能有点棘手。基本上,它尝试做的是为每封电子邮件获取前5个message_details,然后选择那些失败次数等于5的邮件。

Haven没有时间测试这段代码。

SELECT *, count(thirdTable.id) as failTimes FROM
(SELECT rs.id, rs.Status, rs.email
    FROM (
        SELECT *, Rank() 
          over (Partition BY Section
                ORDER BY RankCriteria DESC ) AS Rank
        FROM message_details 
        ) rs WHERE Rank <= 5 ) as thirdTable
Where thirdTable.status = 'FAILED' GROUP BY thirdTable.email HAVING failTimes = 5

答案 1 :(得分:0)

试试这个,

SELECT TOP 5 CampID,COUNT(id) Occ FROM dbo.message_details
WHERE Status    = 'FAILED'
GROUP BY CampID
ORDER BY CampID desc

答案 2 :(得分:0)

这与Using LIMIT within GROUP BY to get N results per group?非常相似,我已尝试过对该帖子的一些回答。您想要获取的是所有电子邮件的最后5个失败发送广告系列,因此您的sql SELECT *, count(id) as occ FROM message_details WHERE status='FAILED' GROUP BY email似乎无法实现。

假设您的数据如下:

message_details
---
| ID | campaign_id | date_sent | date_delivered | email | status | 
| 1 | 1 | 2016-04-21 | null | aaa@aaa.aaa | FAILED | 
| 2 | 2 | 2016-04-22 | null | aaa@aaa.aaa | FAILED | 
| 3 | 3 | 2016-04-23 | null | aaa@aaa.aaa | FAILED | 
| 4 | 4 | 2016-04-24 | null | aaa@aaa.aaa | FAILED | 
| 5 | 5 | 2016-04-25 | null | aaa@aaa.aaa | FAILED | 
| 6 | 6 | 2016-04-26 | null | aaa@aaa.aaa | FAILED | 
| 7 | 1 | 2016-04-21 | null | ddd@aaa.aaa | FAILED | 
| 8 | 2 | 2016-04-22 | null | ddd@aaa.aaa | FAILED | 
| 9 | 3 | 2016-04-23 | null | ddd@aaa.aaa | FAILED | 
| 10 | 4 | 2016-04-24 | null | ddd@aaa.aaa | FAILED | 
| 11 | 5 | 2016-04-25 | null | ddd@aaa.aaa | FAILED | 
| 12 | 6 | 2016-04-26 | null | ddd@aaa.aaa | FAILED | 

然后运行这个sql,

SELECT t1.email, t1.campaign_id
FROM message_details t1
LEFT JOIN message_details t2 ON t1.email = t2.email AND t1.date_sent <= t2.date_sent
WHERE t1.status='FAILED'
GROUP BY t1.email, t1.campaign_id, t1.date_sent
HAVING COUNT(*) <= 5

我得到了以下结果集,

| email | campaign_id | 
| aaa@aaa.aaa | 2 | 
| aaa@aaa.aaa | 3 | 
| aaa@aaa.aaa | 4 | 
| aaa@aaa.aaa | 5 | 
| aaa@aaa.aaa | 6 | 
| ddd@aaa.aaa | 2 | 
| ddd@aaa.aaa | 3 | 
| ddd@aaa.aaa | 4 | 
| ddd@aaa.aaa | 5 | 
| ddd@aaa.aaa | 6 | 

我不知道这是否适合您,但您可以查看上面的链接。如果您的帖子有一些解决方案,请分享给我,我们将不胜感激。