SQL/SSJS count events in timeline

烈酒焚心 提交于 2019-12-08 09:18:08

问题


I've got this two tables, representing Email send and open events:

                  TABLE1 (SENDS)
 ------------------------------------------------
| ReceiptId | JobId |  SK  |       LogDate       |
|-----------|-------|------|---------------------|
|  0000001  | 67789 | 4151 | 6/4/2018 3:21:48 AM |
|-----------|-------|------|---------------------|
|  0000002  | 67789 | 4151 | 6/4/2018 4:15:50 AM |
|-----------|-------|------|---------------------|
|  0000003  | 67789 | 4151 | 6/4/2018 9:42:04 AM |
|-----------|-------|------|---------------------|
|  0000004  | 67789 | 4151 | 6/4/2018 7:28:09 PM |
 ------------------------------------------------

            TABLE2 (OPENS)
 -------------------------------------
| JobId |  SK  |       LogDate        |
|-------|------|----------------------|
| 67789 | 4151 | 6/4/2018 7:31:42 AM  |
|-------|------|----------------------|
| 67789 | 4151 | 6/4/2018 11:32:52 AM |
|-------|------|----------------------|
| 67789 | 4151 | 6/4/2018 2:12:03 PM  |
|-------|------|----------------------|
| 67789 | 4151 | 6/4/2018 5:14:37 PM  |
 -------------------------------------

I need a SQL query or 'Server-Side Javascript Script' to get the count of Opens from TABLE2, where (TABLE1.JobId = TABLE2.JobId & TABLE1.SK = TABLE2.SK), grouped by TABLE1.ReceiptId, considering that the 'open' date will be allways higher than 'send' date, and the last email sent will be the first one on email inbox so will be the first one to be opened by the user.

So we need to match every 'Open' event with the last-sent unopened email, or with the last email sent if there is not any unopened email. 'Sent-Opened' logic is represented on this schema:

So the result table for the two tables above would be:

               RESULT TABLE
 -----------------------------------------
| ReceiptId | JobId |  SK  |  Opens Count |
|-----------|-------|------|--------------|
|  0000001  | 67789 | 4151 |      1       |
|-----------|-------|------|--------------|
|  0000002  | 67789 | 4151 |      1       |
|-----------|-------|------|--------------|
|  0000003  | 67789 | 4151 |      2       |
|-----------|-------|------|--------------|
|  0000004  | 67789 | 4151 |      0       |
 -----------------------------------------

EDIT: Marketing Cloud allows to use SSJS as well. Both SSJS or SQL could be a solution for me.

Thank you very much in advance.

来源:https://stackoverflow.com/questions/50756599/sql-ssjs-count-events-in-timeline

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!