1

I've noticed some quite odd behavior from running queries in automation studio.

Below is the query in question:

SELECT C.id
, CM.id
FROM ent.CampignMember_Salesforce CM 
JOIN ent.Contact_Salesforce C 
ON CM.ContactId=C.Id

We roughly have 100K contacts records in our environment and ~ 500k campign member records related to them.

Thus the expected result of this query should return 500k records. This is because in average for each contact there are 5 campign member record related to it.Therefore we can expect in average 5 duplicates records for each contact id.

The actual result is as follows:

  1. When the target DE has no primary key field set the query returns 500k as expected.
  2. However, When the target DE use contact id as the primary key - instead of getting an error of trying to insert duplicates value for the primary key field - the query runs without an error and chooses randomly 1 of the 5 records for each contact id. In this case the query returns 100k records only.

Note no changes done in query except for the target DE use of primary key.The query is set to OVERWRITE data on target DE.

Any explanation for this odd results? Shouldn't the query result not be affected by the settings of the target DE?

Thanks, Barak

1 Answer 1

1

So if I am understanding your question, the issue is that you have the primary key of ContactId, which limits the results to just a single contact record instead of including each of the correlated campaign records.

To get the 500k result, you would need to either include both ContactId and CampaignId as the primary keys or shift it to just show CampaignId as the primary key.

As to why this happens it is because the SQL will take the target's context into account when saving and/or running the query, which is why in a query activity you need to include both the SQL as well as the target DE you are looking to send results to.

5
  • Gortonington, any reference to this behavior from the documentation? This does not make any sense. On what basis then MC decides which record to keep? Thanks
    – Barak
    Commented Oct 18, 2021 at 19:49
  • 1
    help.salesforce.com/s/articleView?id=000320457&type=1 (If there are duplicates for a primary key. The query will deduplicated on the Primary Key field, and then insert the data as if it were updating. This will only leave one row for each duplicate primary key.) Commented Oct 18, 2021 at 21:22
  • @Barak ^^ forgot to tag you in the comment and waited too long to edit. As a note they give a semi detailed explanation inside the doc. That might help make it make more sense to you. Commented Oct 18, 2021 at 21:27
  • 1
    Thank you so much for providing the relevent article. I still find no logic in this behavior. Is seems UPDATE method will throw an error and will not dedupe automatically but OVERWRITE method will - as I described in the question above. This still does not make any sense why it is designed to behave differently at each scenario. Another question raised by the article provided is - how it's decided what is the 'first' record found and what is the 'second' record found? Does the system searches the records by the order they appear in a DE? Thanks again
    – Barak
    Commented Oct 19, 2021 at 18:16
  • @Barak Yeah, its the first one that is viewed is the 'add' record. and then every record with same key after that is 'update' record that overwrites the info on the original add record. Commented Oct 19, 2021 at 22:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .