
|
I have 2 columns (Account Number & State) with follwing set of records in my table
Acc No State
1001 WA
1002 NY
1002 NY
1002 NY
1003 CA
1003 CA
1001 CA
1002 NY
I want to pull such Account Numbers that has multiple states associated with that account. Looking at the existing records, output should be as shown below
1001
1002
What query should I write to get this result set?
Regards,
Vipul Mehta
|
|
|
|

|
My bad - I did not look close enough at the answer.
Never underestimate the power of human stupidity
RAH
|
|
|
|

|
Nice answer. +5
can I suggest that you try to format your code so that it is easier to read?
i.e.
SELECT DISTINCT(col1)
FROM t1
GROUP BY col1,col2
HAVING COUNT(col1) < 2
Lobster Thermidor aux crevettes with a Mornay sauce, served in a Provençale manner with shallots and aubergines, garnished with truffle pate, brandy and a fried egg on top and Spam - Monty Python Spam Sketch
|
|
|
|

|
Why do you expect "1002" to be among the output? Only "NY" is associated with "1002", though 4 times. But you said "multiple states".
|
|
|
|

|
select [Acc No],count(*) from table group by [Acc No] having count(*)>1
|
|
|
|

|
SELECT DISTINCT A.Acc_No,count(*)
FROM [Test].[dbo].[Table_2] A
JOIN [Test].[dbo].[Table_2] B
on A.Emp_state = B.Emp_state
group by A.Acc_No having COUNT(*)>1
|
|
|
|