I have 2 tables, one that stores a list of emails newsletters, and another that stores information on what users have opened what email. 1 user can open an email more than once, and it will be written to the table each time. There will be some areas where I want to see how many times each user opened the email, but for this query I only want to see how many distinct users opened the email. Below is the code to create a simplified version of the tables, and the query I have so far:CREATE TABLE Emails ( emailID int, subject varchar(250), body varchar(1000))GOINSERT INTO Emails (emailID, subject, body) VALUES (1,'Subject line', 'Newsletter body')GOCREATE TABLE EmailOpens ( openID int, emailID int, subscriberID int)GOINSERT INTO EmailOpens (openID, emailID, subscriberID) VALUES (1,1,1)GOINSERT INTO EmailOpens (openID, emailID, subscriberID) VALUES (2,1,2)GOINSERT INTO EmailOpens (openID, emailID, subscriberID) VALUES (3,1,1)GOSELECT e.emailID, e.subject, opens = (SELECT COUNT(subscriberID) FROM EmailOpens eo WHERE e.emailID = eo.emailID)FROM emails eGODROP TABLE EmailOpensGODROP TABLE EmailsGO
The problem with this is that it returns that there has been 3 opens, when I only want to show that the email has been opened by 2 users (I also have the number of recipients and want to work out the percentage of emails opened).Any ideas?