问题
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