0

I have a mysql row that I am trying to get distinct number of email addresses grouped by province. And it seems to be giving weird values.

If I do the following (count distinct email addresses for province) for each province

SELECT COUNT(DISTINCT(email_address)) as COUNT 
FROM entries 
WHERE subscribe='1' 
AND province='ns'

and add them all up. I get a number that is roughly 200 more then if I just do

SELECT COUNT(DISTINCT(email_address)) as COUNT 
FROM entries 
WHERE subscribe='1'

which also matches

SELECT COUNT(DISTINCT(email_address)) as COUNT 
FROM entries 
WHERE subscribe='1' 
AND province IN('ns','nb','pei')

but doesn't match

SELECT COUNT(DISTINCT(email_address)) as COUNT 
FROM entries 
WHERE subscribe='1' 
GROUP BY province

if I do

SELECT COUNT(DISTINCT(email_address)) as COUNT 
FROM entries 
WHERE subscribe='1'

So the 2nd one and the 4th one match, and the 1st and 3rd and 5th ones match. But the 2nd/4th is 200 more then the 1st/3rd/5th.

All I want is number of distinct email addresses where subscribe is 1 grouped by province. And it seems to be impossible to get.

4

1 回答 1

0

is it possible that an email address is tied to more than one province? You can do a query to test this

SELECT email_address, count(distinct province)
FROM entries
WHERE subscribe = '1'
GROUP BY email_address HAVING count(distinct province) > 1

That may be where your discrepancy is coming in

Either way, this query will give you what you're looking for

SELECT province, COUNT(DISTINCT email_address) as COUNT 
FROM entries 
WHERE subscribe = '1' 
GROUP BY province
ORDER BY province
于 2013-03-11T18:00:32.573 回答