If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Retrieve duplicate rows

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-15-11, 11:44
homiesite homiesite is offline
Registered User
 
Join Date: Aug 2011
Posts: 2
Retrieve duplicate rows

Hi,

I am trying to retrieve just the rows that have a duplicate column value from a distinct result set.
For example.....

If I do SELECT DISTINCT col1, col2 FROM table I get....

col1 col2
1...... a
2.......b
2...... c
3...... d

From these results I am trying to retrieve just the rows that have duplicates in col1, in this case just the rows where col1 has a value of 2. After everything I have tried, I can't get just these 2 rows. Any help would be greatly appreciated. Thanks in advance.

Regards,
Jay
Reply With Quote
  #2 (permalink)  
Old 08-15-11, 13:46
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Something like this:
Code:
SELECT col1
FROM the_table
GROUP BY col1
HAVING count(*) > 1
Reply With Quote
  #3 (permalink)  
Old 08-15-11, 14:12
Pat Phelan Pat Phelan is online now
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
It seems that r927 and shammat are solving a different problem than what I'm reading. My suggestion would be:
Code:
SELECT DISTINCT col1, col2
   FROM table AS a
   WHERE EXISTS (SELECT *
      FROM table AS b
      WHERE  b.col1 = a.col1
         AND  b.col2 <> a.col2)
-PatP
__________________
In theory, theory and practice are identical. In practice, theory and practice are unrelated.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On