views:

71

answers:

4

Trying to select records that are all for the same customer, but where the address is different.

So I can later let the user choose Bob Yonkers, then choose to update all of Bob's records to a specific address. So I want to show all the available records.

Data Example:

CUSTOMER_NAME, CUSTOMER_ADDRESS
Bob Yonkers  , 42 Satellite Cir
Bob Yonkers  , 667 Orbit St
Bob Yonkers  , 42 Satellite Cir
Bob Yonkers  , 667 Orbit St
David Boom   , 5959 Bush Ave
David Boom   , 5959 Bush Ave
David Boom   , 5959 Bush Ave
David Boom   , 5959 Bush Ave
David Boom   , 5959 Bush Ave
Ruby Tuesday , 123 Highway Ln Apt#1
Ruby Tuesday , 123 Highway Ln
David Boom   ,5959 Bush Ave
David Boom   ,5959 Bush Ave
David Boom   ,5959 Bush Ave

So the query would bring back these results...

Result Example:

CUSTOMER_NAME, CUSTOMER_ADDRESS
Bob Yonkers  , 42 Satellite Cir
Bob Yonkers  , 667 Orbit St
Ruby Tuesday , 123 Highway Ln Apt#1
Ruby Tuesday , 123 Highway Ln

Any help would be appreciated.

+4  A: 
SELECT * 
FROM [table] t1
INNER JOIN [table] t2 ON t1.Name=t2.Name AND t1.Address<>t2.Address
Joel Coehoorn
I think you need a SELECT DISTINCT, otherwise you'll get repetitions in the example case.
Alex Martelli
This one is close, but it brings back all four of Bob Yonkers, instead of just the two different addresses.
Mastro
A: 

give this a try...

select * from (select count(customername) as ct, customername, address from table group by customername, address) t1
where t1.ct>1
jle
+1  A: 

This is a refinement of Joel's:

SELECT distinct t1.* 
FROM [table] t1
INNER JOIN [table] t2 ON t1.Name=t2.Name AND t1.Address<>t2.Address
RBarryYoung
Thanks that was it!!
Mastro
A: 
lenkite