SQL Find Dups

Status
Not open for further replies.

Coleman

Ars Legatus Legionis
16,790
Subscriptor
Hmm...I would do this with an aggregate function at a glance. Something like:
SELECT DISTINCT dupField, Count(dupField) As 'count'
FROM dupTable
GROUP BY dupField
HAVING (Count(dupField) > 1)

The goal here would be to return the value of the field where the resulting count of each item results in more then 1 value. I've done it before, works ok, but isn't the speediest query if it's a big table because of the count function.

Anyway, a little more info about the field is important. If you are dealing with a string, you may have to do some extra work to eliminate case differences, and then there is always the problem of entry errors(things that should be flagged as errors that a computer won't pickup because it doesn't understand that there is a typeo and 2 characters are juxtaposed). So you probably won't be able to get all of them programmatically if it's got any size to the table.

EDIT: Went and messed with the query will quick to get it right...that or something like that should more or less work.
-J

[This message was edited by colema19 on December 04, 2001 at 08:10.]
 

Coleman

Ars Legatus Legionis
16,790
Subscriptor
It occurs to me that what you are trying to do, you can basically replace the contents of that field it sounds like, without impacting the integrety of other tables. It might be simpler to just export your data fields, and then reimport them into a new table only one with a primary key, and let it generate new PKs rather then try to sort out the existing.

Do you actually need to preserve those values for anything?
-Jason
 

reboot

Ars Legatus Legionis
10,818
couple way really, play around with them


code:<HR>
Code:
select (duplicate field names) from table_name 
group by (list out fields) having 
count(*)>1;

code:<HR>
Code:
delete from table a where rowid > ( 
select min(rowid) from table b where 
a.column1 = b.column2);

code:<HR>
Code:
DELETE FROM table_name A 
WHERE A.rowid > ANY (SELECT B.rowid 
FROM table_name B 
WHERE A.col1 = B.col1 
AND A.col2 = B.col2 
AND etc... 
) 
;


Also what I like to do is to is create a constraint and dump all the exceptions to a temp table then delete from table1 where table1.key = temptable.key

Sorry I don't have any pre-made SQL for that but it should be easy to find on the web...

That's kinda along the lines of what colema19 suggested.

Edit : indeed it was easy to find :

http://www.jlcomp.demon.co.uk/faq/duplicates.html

Edit2 : Oh ! you're probably not on Oracle, so forget that last one..
 

jfmiller28

Ars Centurion
206
Subscriptor
Ok maybe a little more explination will help.

I have a list of case # with accompining information that were originally entered into a table in WordPerfect. My job is to move this to Access. In order to do this I need to find the (48) cases that have multiple entries and go ask the Officer assigned to the case what is really going on. I therefore need a list of any case that appears more than once because this represents inconsistant data which I have the joyful pleasure of straightening out. :rolleyes:

A modified form of the first suggestion worked (thanks!) Now all I have to do is fix the WetWare problems in my data.

Thanks
JFMILLER
 
Status
Not open for further replies.