Subsribe to our RSS

latest reactions

christian louboutin shoes
With that kind of traffic you want to ad …
Rahul Anand
Thanks for the nice post. It works for m …
Serkan
thx a lot! …
msb
Thanks for above solution.There is ano …
Lori S.
FYI, I was using this successfully in CF …

Use OpenDNS

mxna feeds

cfmailparam behaviour change in CF9.01ColdFusion Job Opportunity in Houston, TXFacial Recognition in 14 Lines Of ColdFusionColdFusion UPS PackageColdFusion Job Opportunity in Arlington, VAColdFusion Job Opportunity in San Diego, CASome ColdFusionBloggers.org GuidelinesColdFusion Positions In CaliforniaListChangeDelimsunix epoch with coldfusionQuery scripting bug in Coldfusion 9Scoth On The Rock 2011 Tickets on salefoursquareCFC updated to version 1.0, now includes ColdFusion return typeFramework One Birds of a Feather session at CFUnitedWhat makes you a good ColdFusion programmer?

All files are strictly confidential: all information is classified.
© Copyright 2002 - 2010 mximize.com.
All right reserved.

MXNA webfeed

Visit Carlos Gallupa

How to find duplicate values in a table?

With the SQL statement below you can find duplicate values in any table, just change the tablefield into the column you want to search and change the table into the name of the table you need to search.

In your recordset you will see the tablefield and how many times it is found as a duplicate.

SELECT     tablefield, COUNT(tablefield) AS dup_count
FROM         table
GROUP BY tablefield
HAVING     (COUNT(tablefield) > 1)

Some further tempering with the statement gets the complete records that are double. (yeah yeah.. no * should be used in the SELECT) It's just for demonstrating folks!!

SELECT *
FROM table
WHERE tablefield IN (
 SELECT tablefield
 FROM table
 GROUP BY tablefield 
 HAVING (COUNT(tablefield ) > 1)
)

To go even further in the process and DELETE every double record we could do something like make a temporary table, insert the double records, delete it from the original table and insert the saved single records from the temporary table.

23850 viewed | 12 opinion(s)  | del.icio.us | Digg it | Tjarko @ 01/12/05 23:56 cet


Reactions:

dynk wrote.... (site)

Thanks for the SQL help. Also, I like your captcha.

11 July 2007 16:17 cet  

Andy wrote.... (site)

thanks for the solution.

20 December 2007 14:08 cet  

Ben wrote.... (mail)

I used you code but couldn't find the duplicate, but using my code, I get:

select count (*) from table = 121
select count (distinct(tableField)) from table = 120

Why am I not able to return the duplicate record using your code?Please help.

14 February 2008 6:02 cet  

pagla wrote.... (site)

wow. thanx a lot

24 October 2008 5:47 cet  

Ismail wrote....

Genius!

18 November 2008 0:00 cet  

Alex Sava wrote.... (mail)

What if I have 4 table fields that have to be identical in order to have a duplicate value?
Like in a clients table the name, street address, number and town have to be the same to have a duplicate client.

And i want to modify the second select so i can have the complete record (select *) that are duplicate.

Thanks a bunch.

11 March 2009 10:07 cet  

Fredric wrote.... (mail)

A faster approach.

I first tried your query when searching for duplicates in a database with some 9000+ records, but the query took a very long time finish (about 15 minutes on my workstation). The table I was working with was made up of two integer columns and one variable length character (varchar) column (all with indices I might add).

Columns: id (int), country (int), name (varchar).

I wanted to find records that had different IDs but were otherwise the same.

The following query takes but a fraction of the time to finish (0.3 seconds on my workstation):

SELECT t1.*
FROM towns AS t1
LEFT JOIN towns AS t2
ON t1.id != t2.id
AND t1.country = t2.country
AND t1.name = t2.name
WHERE t2.id IS NOT NULL
GROUP BY t1.id

I hope it helps!

03 April 2009 11:00 cet  

Tjarko wrote.... (site)

Nice one!! I'm reading transact SQL cookbook at the moment and these kind of solutions are far better than my old one (2005) :-)

03 April 2009 11:24 cet  

Gokul wrote....

Thanks a lot!

05 May 2009 8:58 cet  

Atea Web wrote.... (site)

Was looking for this, very simple, thank you!

14 November 2009 22:40 cet  

Red wrote.... (site)

Genius! Answered my question today three years ago! :D Thanks man!

22 May 2010 7:34 cet  

msb wrote.... (site)

Thanks for above solution.

There is another way to get the duplicate values in table; assuming that the field has a uniqe ID:

SELECT DISTINCT a.tablefield Table AS a INNER JOIN Table AS b ON a.tablefield = b.tablefield WHERE a.tablefieldID <> b.tablefieldID


Hope this helps too

14 June 2010 18:10 cet  

Leave your comment

Your name


Your url/website/link/email....


Some room for your reaction is placed here..



The answer to the ultimate question is?? (42 ;-))




URL en mail addresses are translated for you... life sometimes is that simple!!