SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query help
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

thebrenda
Starting Member

22 Posts

Posted - 10/12/2012 :  05:35:46  Show Profile  Reply with Quote
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 account
111111 1
111111 1
111111 2
222222 2
333333 3
333333 4
444444 4

bandi
Flowing Fount of Yak Knowledge

India
2168 Posts

Posted - 10/12/2012 :  05:46:27  Show Profile  Reply with Quote

DECLARE @tab TABLE(customer bigint, account int)
INSERT INTO @tab
SELECT 111111, 1 UNION ALL
SELECT 111111, 1 UNION ALL
SELECT 111111, 2 UNION ALL
SELECT 222222, 2 UNION ALL
SELECT 333333, 3 UNION ALL
SELECT 333333, 4 UNION ALL
SELECT 444444, 4
SELECT account, MIN(customer) FROM @tab
GROUP BY account
HAVING COUNT(distinct customer) > 1


--
Chandu
Go to Top of Page

webfred
Flowing Fount of Yak Knowledge

Germany
8679 Posts

Posted - 10/12/2012 :  05:46:49  Show Profile  Visit webfred's Homepage  Reply with Quote
Try this:

select distinct account
from YourTable as t1
where 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.

Edited by - webfred on 10/12/2012 05:50:29
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000