I'm facing a challenge with my SQL query in Marketing Cloud and would appreciate the community's help in resolving it.
Recently, I created three separate Data Extensions as part of a segmentation. To achieve this, I ran three different SQL queries. The goal is to merge these three queries into a single query to store the resulting data in a single Data Extension.
Here are the three queries I used:
-
Select FirstName, LastName, city, Subscriberkey, EmailAdress FROM [Segment1] WHERE City IS NULL OR City NOT LIKE '%Denver%'
-
Select FirstName, LastName, city, Subscriberkey, EmailAdress FROM [Segment1] WHERE City LIKE '%Denver%' and EmailAddress NOT IN (Select EmailAddress from Contacts_segment)
-
Select FirstName, LastName, city, Subscriberkey, EmailAddress from [Segment2] WHERE EmailAddress NOT IN (Select EmailAddress from [Contacts_segment] UNION Select EmailAddress from [Contact_data])
Here is my attempt to combine these 3 queries into a single query:
SELECT
FirstName, LastName, city, Subscriberkey, EmailAddress
'Segment1' AS Segment
FROM [Segment1]
WHERE City IS NULL
OR City NOT LIKE '%Denver%'
UNION
SELECT
FirstName, LastName, city, Subscriberkey, EmailAddress
'Segment2' AS Segment
FROM [Segment1]
WHERE City LIKE '%Denver%'
UNION
SELECT
FirstName, LastName, city, Subscriberkey, EmailAddress
'Segment3' AS Segment
FROM [Segment2]
Note: The fields in my target Data Extension are the same as the ones I'm selecting in my SELECT statement (FirstName, LastName, City, Subscriberkey, EmailAddress), with the only difference being the inclusion of a field (Segment) to identify which segment the contacts are coming from. My only primary key is the Subscriberkey to ensure that they are unique contacts.
I'm facing an issue where the final numbers in the resulting Data Extension do not match the sum of the three original Data Extensions. There seems to be some problem with my final query, but I can't identify it. The situation may be a bit confusing, but I'm confident that someone here can help me resolve this issue. Any suggestions or guidance would be greatly appreciated. Thank you for your patience, I'm new to Marketing Cloud and I'm trying to learn more.
EmailAdress
a typo?