Click here to Skip to main content
       

Database

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionGet records having different data in 2 rowsmemberVipul Mehta5 Aug '12 - 19:14 
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

GeneralRe: Get records having different data in 2 rows PinmemberMycroft Holmes6 Aug '12 - 23:39 
My bad - I did not look close enough at the answer.
Never underestimate the power of human stupidity
RAH

GeneralRe: Get records having different data in 2 rows PinmemberSimon_Whale6 Aug '12 - 22:21 
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

AnswerRe: Get records having different data in 2 rows PinmemberBernhard Hiller6 Aug '12 - 21:45 
Why do you expect "1002" to be among the output? Only "NY" is associated with "1002", though 4 times. But you said "multiple states".
AnswerRe: Get records having different data in 2 rows PinmemberNiju114 Aug '12 - 1:15 
select [Acc No],count(*) from table group by [Acc No] having count(*)>1
AnswerRe: Get records having different data in 2 rows PinmemberKarthik_J, Coimbatore29 Aug '12 - 0:02 
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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web02 | 2.6.130516.1 | Last Updated 17 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid