Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Community Wiki

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Navigation

Google Ads

Finding duplicates across columns

From Wiki

Jump to: navigation, search

You have a table with a bunch of columns and you would like to return all the rows where a combination of columns has duplicate data. So how can you do this? It is pretty easy, you do a group by followed by a count having > 1

First create this table

  1. CREATE TABLE #Dups(
  2. id INT IDENTITY,
  3. Col1 VARCHAR(20),
  4. Col2 VARCHAR(20),
  5. Col3 VARCHAR(20),
  6. Col4 VARCHAR(20),
  7. Col5 VARCHAR(20),
  8. SomeOtherCol VARCHAR(20))

Insert this data

  1. INSERT #Dups VALUES('a','a','a','a','a','a')
  2. INSERT #Dups VALUES('a','a','a','a','a','a')
  3. INSERT #Dups VALUES('b','a','a','a','a','a')
  4. INSERT #Dups VALUES('b','a','a','a','a','a')
  5. INSERT #Dups VALUES('b','b','b','b','b','b')
  6. INSERT #Dups VALUES('c','a','a','a','a','a')
  7. INSERT #Dups VALUES('d','a','a','a','a','a')
  8. INSERT #Dups VALUES('e','a','a','a','a','a')
  9. INSERT #Dups VALUES('f','a','a','a','a','a')
  10. INSERT #Dups VALUES('g','a','a','a','a','a')
  11. INSERT #Dups VALUES('a','g','a','a','a','a')


  1. SELECT *
  2. FROM #Dups


id Col1 Col2 Col3 Col4 Col5 SomeCol
1 a a a a a a
2 a a a a a a
3 b a a a a a
4 b a a a a a
5 b b b b b b
6 c a a a a a
7 d a a a a a
8 e a a a a a
9 f a a a a a
10 g a a a a a
11 a g a a a a


Here is the query

  1. SELECT Col1,Col2,Col3,Col4,Col5,MIN(id),MAX(id)
  2. FROM #Dups
  3. GROUP BY Col1,Col2,Col3,Col4,Col5
  4. HAVING COUNT(*) > 1


Col1 Col2 Col3 Col4 Col5 MinMax
a a a a a 1 2
b a a a a 3 4


As you can see the duplicate data is displayed


Contributed by: --SQLDenis 03:03, 31 May 2008 (GMT)

Part of SQL Server Programming Hacks

Section Handy tricks

287 Rating: 1.8/5 (8 votes cast)