为什么“有”条款不起作用?

时间:2017-12-04 17:28:03

标签: sql sql-server

对于以下查询:

select count(distinct email_address) 
from 
(
select distinct email_address, 
case when elq_activity_type='EmailSend' then 1 else 0 end 'Sends', 
case when elq_activity_type='Bounceback' then 1 else 0 end 'Bounces', 
case when elq_activity_type='EmailOpen' then 1 else 0 end 'Opens', 
case when elq_activity_type='EmailClickthrough' then 1 else 0 end 'Clicks' 
from elq_stg_activity
) a
having sum(sends-bounces)>0

having子句似乎没有做任何事情。我究竟做错了什么? 需要获取向其发送电子邮件的所有唯一电子邮件(发送 - 退回)。 谢谢!

1 个答案:

答案 0 :(得分:1)

我想你想要这个:

select count(email_address) 
from (select email_address, 
             sum(case when elq_activity_type = 'EmailSend' then 1 else 0 end) Sends, 
             sum(case when elq_activity_type = 'Bounceback' then 1 else 0 end) as Bounces, 
             sum(case when elq_activity_type = 'EmailOpen' then 1 else 0 end) as Opens, 
             sum(case when elq_activity_type = 'EmailClickthrough' then 1 else 0 end) as Clicks 
      from elq_stg_activity
      group by email_address
     ) a
where sends = bounces;

您的查询存在许多问题。这是我能想到的唯一明智的解释。