Jump to content

Group By


SchweppesAle

Recommended Posts

Trying to figure out what's wrong with the following query. 

 

doesn't work(returns an empty set)

SELECT id, email, 
COUNT(email) AS NumOccurrences    
FROM huge_email_list 
GROUP BY id  
HAVING ( COUNT(email) > 1 ) 

 

works just fine

SELECT id, email, 
COUNT(email) AS NumOccurrences    
FROM huge_email_list 
GROUP BY email 
HAVING ( COUNT(email) > 1 ) 

Link to comment
Share on other sites

For the first one, if id is a unique index, your groups will only have one row in each of them and the count() will never be greater-than one.

 

hmmm..I actually thought GROUP BY was used to return only unique entries for the specified column.  So "GROUP BY email" would only return one entry which contains that specific email. 

 

That's why I had assumed GROUP BY id was unnecessary since it's an index.  Is this not the case?

 

Link to comment
Share on other sites

It might be more useful if I describe exactly what I'm trying to do.  We have a huge table containing email addresses(>5million) and I'd like to write a query which singles out duplicate email address entries. 

 

The second query(appears?) to work but due to the GROUP BY email clause it won't display all duplicate entries for a specific email address and their associated ids.  I'd like to modify it slightly so that it displays all duplicate entries per email address. 

 

What would be the correct way of doing this?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.