Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 removing repeats for duplicates

Author  Topic 

dcronin
Starting Member

1 Post

Posted - 2006-01-20 : 12:23:32
I am trying to find duplicate names in my entity table using the following code:

select e1.entity_key, e2.entity_key, e1.name
from entity e1, entity e2
where e1.name=e2.name
and e1.entity_key!=e2.entity_key
group by e1.entity_key, e2.entity_key, e1.name
order by e1.entity_key

It works fine for single duplicates as follows:

entity1 key entity2 key entity1 name entity2 name
4299 4297 NG, MARIE NG, MARIE
4297 4299 NG, MARIE NG, MARIE

It's not so great for three or more as follows:

entity1 key entity2 key entity1 name entity2 name
4426 4420 PALOMINO, MARK PALOMINO, MARK
4431 4420 PALOMINO, MARK PALOMINO, MARK
4420 4426 PALOMINO, MARK PALOMINO, MARK
4431 4426 PALOMINO, MARK PALOMINO, MARK
4420 4431 PALOMINO, MARK PALOMINO, MARK
4426 4431 PALOMINO, MARK PALOMINO, MARK

How can I modify the sql to eliminate the entity1 repeats in those cases?

nosepicker
Constraint Violating Yak Guru

366 Posts

Posted - 2006-01-20 : 12:36:23
Something like this?

SELECT e1.* FROM entity AS e1
JOIN (SELECT entity.name FROM entity GROUP BY entity.name HAVING COUNT(*) > 1) AS e2
ON e1.name = e2.name
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-01-23 : 02:51:47
Also refer
http://sqlteam.com/forums/topic.asp?TOPIC_ID=6256

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -