We would like to get the sends, (unique) opens and (unique) clicks from last 24 hours in the same data extension. I started with 2 LEFT JOINS, with the goal of getting all sends, and opens and clicks only where they ocurred. However, I'm stuck because the results are having lots of duplicates (the target data extension does not have a primary key to avoid any PK violations). I followed the data structure as described on SFMC data views help page. This is the query we are using (separately for all three metrics):
Sends
SELECT a.subscriberkey, CONVERT(varchar, EventDate, 20) AS EventDate,
b.emailid, b.emailname, CONCAT(a.Subscriberkey,'_Sent_',b.EmailID,'_',convert(varchar, EventDate, 20) ) AS Event_Id
FROM _Sent AS a
INNER JOIN _job AS b ON a.jobid=b.jobid AND
CAST(a.eventdate as date)>=cast(getdate() -1 as date)
Opens
SELECT a.subscriberkey, CONVERT(varchar, EventDate, 20) AS EventDate,
b.emailid, b.emailname, CONCAT(a.Subscriberkey,'_Open_',b.EmailID,'_',convert(varchar, EventDate, 20) ) AS Event_Id
FROM _open AS a
INNER JOIN _job AS b ON a.jobid=b.jobid
AND CAST(a.eventdate as date)>=cast(getdate() -1 as date) WHERE a.IsUnique = 1
Clicks
SELECT a.subscriberkey, CONVERT(varchar, EventDate, 20) AS EventDate, b.emailid,
b.emailname, a.LinkContent AS URL, a.LinkName AS Alias, a.LinkContent AS Details,CONCAT(a.Subscriberkey,'_Click_',
b.EmailID,'_',convert(varchar, EventDate, 20) ) AS Event_Id
FROM _click AS a
INNER JOIN _job AS b ON a.jobid=b.jobid
AND CAST(a.eventdate as date)>=cast(getdate() -1 as date)
WHERE a.IsUnique = 1