这是我的问题:
SELECT
dbo.EmailCampaignTracking.emailOpened,
dbo.EmailCampaignTracking.emailUnsubscribed,
dbo.EmailCampaignTracking.emailBounced,
COUNT(*)
FROM
dbo.EmailCampaignTracking
Group By
dbo.EmailCampaignTracking.emailBounced,
dbo.EmailCampaignTracking.emailUnsubscribed,
dbo.EmailCampaignTracking.emailOpened
以下是我的结果:
emailOpened emailUnsubscribed emailBounced Totals
True False False 6
False False True 1
True True False 2
我希望电子邮件opend的总数为8而不是6.我意识到我要求SQL将它们分组。我只是想知道如何为每一列获得Distict总数。因此,emailOpened为8,emailUnsubscribed将为1,emailBounced为2.谢谢
答案 0 :(得分:5)
SELECT Count(emailOpened) AS OpenedCount,
Count(emailUnsubscribed) as UnsubCount,
Count(emailBounced) as BouncedCount
FROM dbo.EmailCampaignTracking
编辑:如果您希望在标志为真时计算记录,可以按以下方式编写
SELECT SUM(CASE emailOpened WHEN true then 1 else 0) AS OpenedCount,
SUM(CASE emailUnsubscribed WHEN true then 1 else 0) as UnsubCount,
SUM(CASE emailBounced WHEN true then 1 else 0) as BouncedCount
FROM dbo.EmailCampaignTracking
EDIT2:在上面的查询中,将true
替换为被解释为true的位值(可能为0)。
答案 1 :(得分:2)
我认为你只想计算值设置为true的记录。如果是这样,那么这可能会起作用:
SELECT SUM(CAST(EmailOpened AS INT)) AS OpenedEmails,
SUM(CAST(EmailUnsubscribed AS INT)) AS UnsubscribedEmails,
SUM(CAST(EmailBounced AS INT)) AS EmailBounced
FROM dbo.EmailCampainTracking