Created
January 17, 2012 09:45
-
-
Save bonyiii/1625914 to your computer and use it in GitHub Desktop.
find duplicatet values in a table with SQL
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # http://www.mximize.com/how-to-find-duplicate-values-in-a-table- | |
| # http://www.petefreitag.com/item/169.cfm | |
| 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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment