我的服务中有一个电子邮件递送系统(对于我的电子邮件订阅者)。我需要获取有关已发送邮件,点击次数和我的电子邮件的开放次数的统计信息。所有信息都存储在 PostgreSQL 数据库中。
我的订阅者列表(subscriber_collections
):
_id name
===========
1 MySubscribers
2 VIP Subscribers
所以我有一个表存储每个订阅者列表的电子邮件(emails
,一对多关系)
_id subscriber_collection_id email_address
===========================================
1 1 john.smith@email.com
2 1 james3@email.com
3 1 jay10@email.com
4 2 second@email.com
5 2 john.smith@email.com
6 2 jay10@email.com
我还有一个表mail_activities
,它存储有关不同电子邮件活动的信息(例如点击,打开,发送等等)。:
_id message_id message_subject activity_date email_address type
============================================================================
1 25 Hello msg! 2017-11-21 john.smith@email.com Sends
2 25 Hello msg! 2017-11-21 john.smith@email.com Opens
3 25 Hello msg! 2017-11-21 john.smith@email.com Clicks
4 25 Hello msg! 2017-11-21 jay10@email.com Clicks
5 25 Hello msg! 2017-11-21 jay10@email.com Clicks
6 16 Discount! 2017-11-21 second@email.com Opens
7 16 Discount! 2017-11-21 second@email.com Opens
8 16 Discount! 2017-11-21 james3@email.com Opens
请注意,用户可以在同一封电子邮件中重复点击(或打开),信息将在数据库中。
我有一个SQL查询,它可以计算每个电子邮件和订阅者集合的不同活动:
SELECT
MA.message_id
,MA.message_subject
,SC.name as subscriber_collection_name
,MA.date AS activity_date
,SUM(CASE WHEN MA.type='Sends' THEN 1 ELSE 0 END) as sends_count
,SUM(CASE WHEN MA.type='Opens' THEN 1 ELSE 0 END) as opens_count
,SUM(CASE WHEN MA.type='Clicks' THEN 1 ELSE 0 END) as clicks_count
FROM
mail_activities MA
INNER JOIN emails E ON MA.email_address = E.email_address
INNER JOIN subscriber_collections SC ON E.subscriber_collection_id = SC._id
GROUP BY
MA.message_id
,MA.message_subject
,SC._id
,SC.name
,MA.date
ORDER BY
message_id
此查询正常。以下是此数据的结果:
问题:我需要获得唯一点击次数并打开每个email_address
的活动。我需要将此信息作为该查询中的其他列(unique_clicks
和unique_opens
)获取。例如,表Hello msg!
中用户jay10@email.com
对字母emails
的两次点击应视为一次。如何在其他列中获取此信息?感谢...
P.S:抱歉我的英语不好......
答案 0 :(得分:1)
如果我理解正确,您可以使用条件count(distinct)
:
COUNT(DISTINCT CASE WHEN MA.type = 'Sends' THEN ma.message_id END) as sends_unique_count
COUNT(DISTINCT CASE WHEN MA.type = 'Opens' THEN ma.message_id END) as opens_unique_count
COUNT(DISTINCT CASE WHEN MA.type = 'Clicks' THEN ma.message_id END) as clicks_unique_count
我不太确定这如何适合您现有的查询。您希望每封电子邮件都有唯一的计数(似乎是说明),但您要按message_id
进行汇总。
COUNT(DISTINCT CASE WHEN MA.type = 'Sends' THEN ma.email_address END) as sends_unique_count
COUNT(DISTINCT CASE WHEN MA.type = 'Opens' THEN ma.email_address END) as opens_unique_count
COUNT(DISTINCT CASE WHEN MA.type = 'Clicks' THEN ma.email_address END) as clicks_unique_count