I have a table which has a bunch of entries (standard stuff, emails, name, city, etc) along with a subscribe option for notifying them about future stuff.
I am trying to find out how many for each province opt’d in (have a 1 for the subscribe field). And I am also trying to export that giant list distinct email address.
So I have the following two queries
Get all the values from the table for exporting.
SELECT DISTINCT(email_address), first_name, last_name, street_address, city, province, postal_code, phone FROM entries WHERE subscribe='1' GROUP BY email_address
Get a count (looks like province | #) from the table.
SELECT province, COUNT(DISTINCT email_address) FROM entries WHERE subscribe='1' GROUP BY province;
Now my problem is the first query returns 1124 results, and the second query adds up to 1136 results. So somewhere 12 entries are coming up.
How can I figure out what’s up or is there an issue with my queries or something?
Your problem is that you have some duplicate e-mail addresses across provinces.
The first query eliminates all duplicate e-mail addresses. The second query would only eliminate duplicate e-mail addresses within a given province
If you don’t care which province a duplicate e-mail is counted for, you can eliminate the duplicates like so: