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.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2002-11-04 : 09:29:08
|
| Chinni writes "I need help from you i want to find the duplicates in a table like thisif a table has four fieldsI want to find duplicates in four feildsThis is the tablecol1 col2 col3 col4 ab cd dc xy ab sc sf fg sx dc ss sf dx dc fc fe ed re fg tu dr te fg luI want the result to be like thisresultab 2dc 2fg 2 or how many they arei could find if it is like thiscol1 col2 col3 col4ab df fg shab df fg shit would give outab 2 Thanks in advance," |
|
|
AndrewMurphy
Master Smack Fu Yak Hacker
2916 Posts |
Posted - 2002-11-04 : 11:43:26
|
| select col1, col2, col3, col4, count (*) from tablename1 group by col1 col2 col3 col4 having count(*) > 1will give you details of duplicate rows.TO DELETE such items....there is an ARTICLE on this site with more info....(forum search...DELETE DUPLICATE) to look for duplicate values in multiple individual columns....select column2, count(*) from (SELECT 'col1', col1, count(*) from tablename1GROUP by col1unionSELECT 'col2', col2, count(*) from tablename1GROUP by col2unionSELECT 'col3', col3, count(*) from tablename1GROUP by col3unionSELECT 'col4', col4, count(*) from tablename1GROUP by col4)group by column2having count(*) > 1this may get you part of the way....(i haven't tried it!!!)...be aware that the UNION clause eliminates duplicate values....therefore that's why I'm putting something unique into each "sub select" so that a result of "ab,2" from the 1st select won't prvent a result of "ab,2" also coming from the 2nd select....the overall select (i think if it works) should aggregate the answers from the subselects.... |
 |
|
|
|
|
|
|
|