Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select only duplicates

Status
Not open for further replies.

peterv6

Programmer
Sep 10, 2005
70
US
I have a table with several columns, including one ID field containing a unique key. What I want to do is select all duplicate records from the table based on one of the columns, say the Item number field. Is there a simple way to do that? I know using unique can give me the non-dupe records, but I don't know how to get only the duplicates.
Thanks!

PETERV
Syracuse, NY &
Boston, MA
 
A simple way? I guess it depends on your definition of "simple." There is no keyword or function to find duplicates, if that's what you're hoping for.

It depends on what you want to do, really. If you just want to find records that are non-unique in one or more particular fields, that's fairly easy to do when you have a unique key to work with. For example, this query will find all records that are not don't have unique values in col1 and col2.
Code:
SELECT t1.* 
FROM yourtable AS t1
WHERE EXISTS 
  (SELECT * 
    FROM yourtable AS t2
   WHERE t1.col1 = t2.col1
     AND t1.col2 = t2.col2
     AND t1.id <> t2.id);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top