Skip to content

Instantly share code, notes, and snippets.

@MyCueCards
Last active February 13, 2019 15:07
Show Gist options
  • Save MyCueCards/3d81d752b9cfa7baf0744d6bbb79c003 to your computer and use it in GitHub Desktop.
Save MyCueCards/3d81d752b9cfa7baf0744d6bbb79c003 to your computer and use it in GitHub Desktop.
In Salesforce Marketing Cloud, this query 1) find agents, who are designated with the number 1, 2) only pulls records with the purchase date of the previous month, 3) calculates age based on purchase date and birthday, 4) changes column names for a few, and 5) populates a column with the family designation based on product name.
SELECT
ade.AgentSettingsID,
ade.AgentNumber,
pde.Email,
pde.Given_Name,
pde.Surname,
DATEDIFF(Year, pde.Date_Of_Birth, pde.Purchase_Date) AS "Age",
pde.Mobile AS "Mobile_Phone",
pde.Home AS "Home_Phone",
pde.MailingStreet AS "Street_Address",
pde.City,
pde.State_Foreign,
pde.State_Name AS "State_Domestic",
pde.Name_Short_English AS "Country",
pde.Effective_Date,
pde.Expiration_Date,
pde.Trip_Start_Date,
pde.Trip_End_Date,
pde.Policy_Name,
CASE
WHEN pde.Policy_Name LIKE 'Partial Value%' THEN ‘Product Family Name Text’
...
ELSE 'ERROR: Not Found'
END as Product_Family
FROM AgentDataExtension ade
INNER JOIN PrimaryDataExtension pde ON pde.Agent_Number = ade.AgentNumber
WHERE ade.AgentSettingsID = 1
AND DATEPART(month,pde.Purchase_Date) = DATEPART(month,DATEADD(month, -1, GETDATE()))
AND DATEPART(year,pde.Purchase_Date) = DATEPART(year,DATEADD(month, -1, GETDATE()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment