I'm not proficient in SQL Query. I'm hoping someone on here can help me write this query for Saleforce Marketing Cloud, which uses a "limited" SQL Server 2016.
Problem: I'm trying to extract month from Appointment Date and count the Appointment Statuses for those months.
- Count the appointment status with 'pending' in month of May
- Count the appointment status with 'canceled' in month of May
- Count the appointment status with 'pending in month of June
- Count the appointment status with 'canceled' in month of June
- Count the appointment status with 'pending' in month of July
- Count the appointment status with 'canceled' in month of July
Use Case: Target customers based on their recent canceled appointment (status = 'canceled') and no upcoming appointment scheduled (status = 'pending')
Data Extensions DE1 = data extension with customer's appointment data - DE1 has records per row of customer's appointments so I need to group the records by CustomerID or Email
DE2 = new data extension with count
Query Here is the query I've worked on so far, but it's not working.
SELECT
SM.Status,
SM.CustomerID,
SM.Email,
SM.AppointmentDate,
sum(CASE WHEN SM.Status = 'Pending' THEN 1 ELSE 0 END) AS LM.May_PendingCount,
sum(CASE WHEN SM.Status = 'Canceled' THEN 1 ELSE 0 END) AS LM.May_CanceledCount
FROM [DE1] SM
inner join [DE2] LM
on SM.Email = LM.Email
WHERE DATEPART(Month, SM.AppointmentDate) = LM.Campaignable
GROUP BY SM.CustomerID