选择绑定到最大列的列

时间:2019-04-03 17:47:54

标签: sql salesforce-marketing-cloud

我正在使用Salesforce Marketing Cloud,并希望创建一个表,其中包含每个订户以及该订户的最后参与情况的详细信息。

现有代码将订阅者及其上次打开的订阅者拉出,或者单击右键,但是我需要链接到另一个表以获取电子邮件的名称。

表结构为:

SUBSCRIBER
+---------+---------------------+
| SubID   | EmailAddress        |
+---------+---------------------+
|       1 | joe@test.com        |
|       2 | jane@test.com       |
+---------+---------------------+

OPENS
+---------------+------------+--------+
| EmailAddress  | EventDate  | JobID  |
+---------------+------------+--------+
| joe@test.com  | 03/04/19   | 1      |
| joe@test.com  | 03/05/19   | 2      |
| joe@test.com  | 03/06/19   | 3      |
| jane@test.com | 03/06/19   | 3      |
+---------------+------------+--------+

CLICKS
+---------------+------------+--------+
| EmailAddress  | EventDate  | JobID  |
+---------------+------------+--------+
| joe@test.com  | 03/04/19   | 1      |
| joe@test.com  | 03/05/19   | 2      |
| joe@test.com  | 03/06/19   | 3      |
| jane@test.com | 03/06/19   | 3      |
+---------------+------------+--------+

JOB
+-------------------------------+
| JobID  | EmailName            |
+--------+----------------------+
| 1      | Spring Sale          |
| 2      | You Are Missing Out  |
| 3      | Please Help          |
+--------+----------------------+

我的目标是使用以下信息更新订户表:

  • 最后一次订婚日期是
  • 参与度是电子邮件打开还是点击
  • 电子邮件的名字是什么

我当前的SQL是:

SELECT
  s.[EmailAddress] AS 'EmailAddress',
  CASE  
    WHEN c.EventDate < o.EventDate THEN o.EventDate
    ELSE c.EventDate
  END AS 'LastEngagedDate',
  CASE  
    WHEN c.EventDate < o.EventDate THEN 'Open'
    ELSE 'Click'
  END AS 'EngagementType'
FROM Subscriber s
INNER JOIN (
  SELECT EmailAddress, MAX(EventDate) AS 'EventDate'
  FROM _Click 
  GROUP BY EmailAddress) c ON c.EmailAddress = s.EmailAddress
INNER JOIN (
  SELECT EmailAddress, MAX(EventDate) AS 'EventDate'
  FROM _Open 
  GROUP BY SubscriberKey) o ON o.EmailAddress = s.EmailAddress

显然,这省略了电子邮件名称。我遇到的问题是将_Click和_Open的适当JobID绑定到MAX(EventDate),以便我可以将JobID加入_Job表以提取适当的电子邮件名称。

请让我知道是否可以进一步澄清此问题。谢谢!

1 个答案:

答案 0 :(得分:0)

像这样使用解析的row_number():

FROM Subscriber s
    left join
            (SELECT EmailAddress, EventDate, JobID    
             row_number() over(partition by EmailAddress order by EventDate desc) rn
            FROM _Click ) c on c.EmailAddress = s.EmailAddress and c.rn=1

请注意,c.rn=1处于联接ON条件下。仅具有最大日期的记录将被合并,并且所有列均可用。与Open事件相同。