I have an table where customers form submissions are being added
API_TABLE
| SubscriberKey | EmailAddress | coupon_code | offer_prefix | submit_date |
|--------------------|--------------------|-------------|---------------|-----------------------|
| email_1@domain.com | email_1@domain.com | ABC-101 | ABC | 6/5/2022 7:03:48 AM |
| email_1@domain.com | email_1@domain.com | ABC-101 | ABC | 6/5/2022 7:03:50 AM |
| email_1@domain.com | email_1@domain.com | ABC-101 | ABC | 6/5/2022 7:03:50 AM |
| email_2@domain.com | email_2@domain.com | ABC-102 | ABC | 6/5/2022 10:01:20 AM |
| email_2@domain.com | email_2@domain.com | XYZ-101 | XYZ | 6/6/2022 9:40:23 AM |
| email_1@domain.com | email_1@domain.com | ABC-101 | ABC | 6/6/2022 11:23:11 AM |
Based on these form submission, an automation is sending an email to these customer and those sent emails are being collected here
SENT_TABLE
| SubscriberKey | EventDate |
|--------------------|-----------------------|
| email_1@domain.com | 6/5/2022 7:03:49 AM |
| email_1@domain.com | 6/5/2022 7:03:51 AM |
| email_1@domain.com | 6/5/2022 7:03:52 AM |
| email_2@domain.com | 6/5/2022 10:02:50 AM |
| email_2@domain.com | 6/6/2022 9:41:23 AM |
| email_1@domain.com | 6/6/2022 11:23:12 AM |
I am creating an automation that queries customer to receive resend next day and currently I am doing this:
SELECT DISTINCT
a.SubscriberKey,
a.EmailAddress,
a.coupon_code,
a.online_prefix,
a.send_date
FROM API_TABLE a
INNER JOIN SENT_TABLE s on s.SubscriberKey = a.SubscriberKey
WHERE s.sendtype = 'API'
AND CAST(s.EventDate AS DATE) = CAST(GETDATE() - 1 AS DATE)
With this query, the issue is - if a customer like "email_2@domain.com" submitted form for 2 different offers (aka offer_prefix), sometimes with the above query, I get record with "submit_date" 6/5/2022 with coupon code from other offer - XYZ-101
like I am getting this
email_2@domain.com | email_2@domain.com | XYZ-101 | XYZ | 6/5/2022 10:01:20 AM
while the submission on 6/5 is from offer_prefix ABC (NOT XYZ)
Desired output:
- Pull ONLY 1 customer record, if there are multiple entries / submissions on the same date for same offer_prefix
- Pull respective offer code & prefix that was submitted originally (not allowing logic to mix coupon code from another offer with submission of different offer - since email address is used as key to join table)
- Pull ONLY 1 customer per date per offer_prefix (if there are multiple entries for same offer, pull only 1 record)