Important alert: (current site time 7/15/2013 8:08:49 PM EDT)
 

VB icon

FindDupes

Email
Submitted on: 11/10/2000 4:00:14 PM
By: Greg 
Level: Beginner
User Rating: By 10 Users
Compatibility: SQL Server 7.0
Views: 21738
 
     Allows you to determine duplicate rows in a table and provide a count of the duplicates. It's helped me find a row or two that had left me with "inconclusive" results in the past : ) Ciao; Greg
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
--**************************************
-- Name: FindDupes
-- Description:Allows you to determine duplicate rows in a table and provide a count of the duplicates.
It's helped me find a row or two that had left me with "inconclusive" results in the past : )
Ciao;
Greg
-- By: Greg
--
-- Assumes:Limiting the columns provides field level duplicate finds..
Using all columns will give a row level duplicate find.
--
--This code is copyrighted and has-- limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=247&lngWId=5--for details.--**************************************

SELECT column1, column2, column3, ... n, COUNT(*) AS [Number of Dupes]
FROM TableName
GROUP BY column1, column2, column3... n 
HAVING COUNT(*) > 1


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
1/30/2001 4:40:38 PMtodde

does anyone know how to find the row in the above statement with the GREATEST NUMBER of dups without using a second query or a view?

todde@metalwest.com
(If this comment was disrespectful, please report it.)

 
1/31/2001 9:36:17 AMGreg

Try the derived table method...

SELECT Column1
FROM (SELECT COLUMN1, COUNT(*) AS 'Amount'
FROM TableName
GROUP BY Column1
HAVING COUNT(*) = (SELECT MAX (B.Amount)
FROM (SELECT COUNT(*) AS 'Amount'
FROM TableName
GROUP BY Column1
HAVING COUNT(*) > 1) AS B)
) AS A
GROUP BY Column1


Hope the formatting is okay.

Ciao;
Greg
(If this comment was disrespectful, please report it.)

 
2/18/2001 3:20:42 AMBrian Bender

Great Job!
Take this one step further. How would you write a query that deletes the dupes and leaves only one behind?
Email me please: brianbender77@hotmail.com
(If this comment was disrespectful, please report it.)

 
2/28/2001 9:23:58 AMGreg

Sorry it took so long...

Deleting data is always risky.

I would suggest multi-staging an approach such as
1) Find dupes
2) query to get PK's based on dupes
3) delete based on PK's -1

Bottom line... thee are many ways this could be accomplished and they depend on the structure of your table(s).

Ciao;
Greg
(If this comment was disrespectful, please report it.)

 
12/10/2002 8:38:46 AM

Thanks, helped a lot!
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.