Reputation:
CustomerName EmailAddress
Aaron [email protected]
Christy [email protected]
Jason [email protected]
Eric [email protected]
Aaron [email protected]
I want to return the values in the table where customerName's are equal and emailaddress are not equal. So for example this table should return.
Aaron [email protected]
Aaron [email protected]
I'm new to sql and i'm unsure how to break this up into checkign first that customernames are the same, and then looking at the emailaddresses
Upvotes: 2
Views: 7479
Reputation: 6202
You accomplish this with a self-join:
Select * from tablename a, tablename b
WHERE a.customername = b.customername
and a.emailaddress != b.emailaddress
Here it is working: http://sqlfiddle.com/#!2/75687/1
You mention only wanting one result. Then you would just modify the above like:
Select Distinct(a.customername) from tablename a, tablename b
WHERE a.customername = b.customername
and a.emailaddress != b.emailaddress
Working version of this: http://sqlfiddle.com/#!2/75687/3
Upvotes: 3
Reputation: 204854
Group by the customername
and then count how namy unique emails you have
select customername
from your_table
group by customername
having count(distinct emailaddress) > 1
If you want also the email in your result you can do
select customername, emailaddress
from your_table
where customername in
(
select customername
from your_table
group by customername
having count(distinct emailaddress) > 1
)
Upvotes: 1