SQL子查询通过/拥有加入/分组

时间:2014-02-09 01:25:41

标签: sql sql-server-2008 join subquery

我正在尝试为电子邮件营销项目编写子查询,下面有两个表(以及我需要的列):

Table: Email_Send:
Column: E-mail_ID
Column: Subject_Line
Column: Send_Date

Table: Email_Event:
Column: Email_ID
Column: Recipient_ID
Column: Recipient_Name
Column: Event_Type

我正在尝试返回Recipient_Name,Event_Types =“Click”,Subject_Line和Send_Date的计数。

我已经让内部查询完美运行了:

SELECT     Email_Event.Recipient_ID, 
           Email_Send.Email_ID, 
           COUNT(Email_Event.Event_Type) 
FROM       Email_Send 
INNER JOIN Email_Event ON Email_Send.Email_ID = Email_Event.EmailID
WHERE      (Email_Event.Event_Type = 'Click') 
AND        (Email_Send.Send_Date > GETDATE() - 3)
GROUP BY   Email_Event.Recipient_ID, Email_Send.Email_ID                      
HAVING     (COUNT(Email_Event.Event_Type) > 4))

这将返回三列,即Recipient_ID,Email_ID和Event_Types计数。在重复的情况下,我必须使用Recipient_ID和Email_ID而不是Recipient_Name和Email_Line。

我尝试了多个子查询,但是如何添加Recipient_Name,Subject_Line和Send_Date以及Count(Event_Type)返回?

0 个答案:

没有答案