How to select each row where the column value DOES NOT APPLY

I need to execute a select statement that returns all rows where the column value is no different (e.g. EmailAddress).

For example, if the table looks like this:

CustomerName EmailAddress Aaron aaron@gmail.com Christy aaron@gmail.com Jason jason@gmail.com Eric eric@gmail.com John aaron@gmail.com 

I need a request to return:

 Aaron aaron@gmail.com Christy aaron@gmail.com John aaron@gmail.com 

I read a lot of posts and tried different queries to no avail. The request, which I believe should work below. Can someone suggest an alternative or say what could be wrong with my request?

 select EmailAddress, CustomerName from Customers group by EmailAddress, CustomerName having COUNT(distinct(EmailAddress)) > 1 
+96
sql sql-server sql-server-2008
Oct 30 '12 at 19:30
source share
8 answers

This is significantly faster than the EXISTS method:

 SELECT [EmailAddress], [CustomerName] FROM [Customers] WHERE [EmailAddress] IN (SELECT [EmailAddress] FROM [Customers] GROUP BY [EmailAddress] HAVING COUNT(*) > 1) 
+176
Oct 11 '13 at 20:26
source share

What is wrong in your request is that you group by email and the name that forms the group of each unique set of email addresses and name combined and therefore

 aaron and aaron@gmail.com christy and aaron@gmail.com john and aaron@gmail.com 

are considered as 3 different groups, and all belong to one separate group.

Please use the query as below:

 select emailaddress,customername from customers where emailaddress in (select emailaddress from customers group by emailaddress having count(*) > 1) 
+32
30 Oct '12 at 19:49
source share

What about

 SELECT EmailAddress, CustomerName FROM Customers a WHERE Exists ( SELECT emailAddress FROM customers c WHERE a.customerName != c.customerName AND a.EmailAddress = c.EmailAddress) 
+9
Oct 30 '12 at 19:34
source share
 select CustomerName,count(1) from Customers group by CustomerName having count(1) > 1 
+7
Jul 03 '13 at 6:29
source share

Just for fun, it's different here:

 ;with counts as ( select CustomerName, EmailAddress, count(*) over (partition by EmailAddress) as num from Customers ) select CustomerName, EmailAddress from counts where num > 1 
+4
Oct 30 '12 at 19:38
source share

Instead of using auxiliary queries, in which a condition that will increase the query time when the records are huge.

I would suggest using Inner Join as the best option for this problem.

Given the same table, this can produce a result.

 SELECT EmailAddress, CustomerName FROM Customers as a Inner Join Customers as b on a.CustomerName <> b.CustomerName and a.EmailAddress = b.EmailAddress 

To get better results, I suggest you use CustomerID or any unique field in your table. Duplication of CustomerName possible.

+4
Sep 19 '16 at 5:29
source share

SELECT [EmailAddress], [CustomerName] FROM [Customers] WHERE [EmailAddress] IN (SELECT [EmailAddress] FROM [Customers] GROUP BY [EmailAddress] HAVING COUNT (*)> 1);

0
Dec 20 '18 at 22:47
source share

Well, there is a slight change to find not clear lines ..

 SELECT EmailAddress, CustomerName FROM Customers WHERE EmailAddress NOT IN (SELECT EmailAddress FROM Customers GROUP BY EmailAddress HAVING COUNT(*) > 1) 
-2
Jan 09 '16 at 23:18
source share



All Articles