Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
thebrenda
Starting Member
22 Posts |
Posted - 2012-10-12 : 05:35:46
|
my data is below. i want to find accounts that are related to more than one customer. each account should only belong to a single customer, but i suspect that is not the case. in my examaple below i want to find accounts 2 and 4. customer account111111 1111111 1111111 2222222 2333333 3333333 4444444 4 |
|
bandi
Master Smack Fu Yak Hacker
2242 Posts |
Posted - 2012-10-12 : 05:46:27
|
DECLARE @tab TABLE(customer bigint, account int)INSERT INTO @tab SELECT 111111, 1 UNION ALLSELECT 111111, 1 UNION ALLSELECT 111111, 2 UNION ALLSELECT 222222, 2 UNION ALLSELECT 333333, 3 UNION ALLSELECT 333333, 4 UNION ALLSELECT 444444, 4SELECT account, MIN(customer) FROM @tabGROUP BY accountHAVING COUNT(distinct customer) > 1--Chandu |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2012-10-12 : 05:46:49
|
Try this:select distinct accountfrom YourTable as t1where exists(select * from YourTable as t2 where t2.account=t1.account and t2.customer <> t1.customer) Too old to Rock'n'Roll too young to die. |
|
|
|
|
|