Solved

SQL Script to find duplicates

Posted on 2016-11-16
16
40 Views
Last Modified: 2016-11-23
Hello,

I have a SQL Database with a table called Products.
Within this table I have a barcode column called ‘Barcode’

I’m want to run a script which will show me all those barcodes which are duplicate.

Does anyone know how to achieve this?

Thank you.
0
Comment
Question by:jspc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
16 Comments
 
LVL 28

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41890861
Try..

SELECT * FROM 
(
	SELECT * , ROW_NUMBER() OVER (PARTITION BY Barcode ORDER BY (SELECT 1)) rnk 
	FROM Barcode
)k
WHERE k.rnk > 1

Open in new window


Or this

SELECT Barcode , COUNT(*) DuplicateCount FROM Products
GROUP BY Barcode 
HAVING COUNT(*) > 1

Open in new window


Hope it helps !!
0
 
LVL 52

Expert Comment

by:Ryan Chong
ID: 41890863
try this:

select Barcode
from Products
group by Barcode
having count(Barcode) > 1

Open in new window

0
 

Author Closing Comment

by:jspc
ID: 41890867
Awesome! Thank you.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41890868
Welcome !!
0
 

Author Comment

by:jspc
ID: 41890871
Oh I also need the column ProductCode and Description included too
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41890874
In below you will get all columns , Just replace * with the columns you want.

SELECT * FROM 
(
	SELECT * , ROW_NUMBER() OVER (PARTITION BY Barcode ORDER BY (SELECT 1)) rnk 
	FROM Barcode
)k
WHERE k.rnk > 1

Open in new window

0
 

Author Comment

by:jspc
ID: 41890876
Sorry but I get this error

Msg 208, Level 16, State 1, Line 1
Invalid object name 'Barcode'.
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41890877
Ohh try...

SELECT * FROM 
(
	SELECT * , ROW_NUMBER() OVER (PARTITION BY Barcode ORDER BY (SELECT 1)) rnk 
	FROM Products
)k
WHERE k.rnk > 1

Open in new window

0
 

Author Comment

by:jspc
ID: 41890882
Thanks.

But this is showing me barcodes with blanks.

I only want to see those duplicate barcodes
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41890884
Ok , Try this... If you dont want blank values..

SELECT * FROM 
(
	SELECT * , ROW_NUMBER() OVER (PARTITION BY Barcode ORDER BY (SELECT 1)) rnk 
	FROM Products
        WHERE LEN(Barcode) > 0
)k
WHERE k.rnk > 1

Open in new window

0
 

Author Comment

by:jspc
ID: 41890885
What does the rnk column mean?
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41890889
I have generated a new column called rnk, which will give us ranking. So if the barcode is duplicate its value will be > 1.

You can ignore it while selecting the final data like bold below.. Here replace * with your columnNames.

SELECT * FROM
(
      SELECT * , ROW_NUMBER() OVER (PARTITION BY Barcode ORDER BY (SELECT 1)) rnk
      FROM Products
        WHERE LEN(Barcode) > 0
)k
WHERE k.rnk > 1
0
 

Author Comment

by:jspc
ID: 41890890
Great. Thanks for your help. All good.
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41890891
Great ! Welcome..
0
 

Author Comment

by:jspc
ID: 41900059
Hello, in regards to the above answer this doesn't show me duplicate product code.

What we are wanting are the two items that have the same barcode, so we can sort out which one belongs to who.
Your query only shows one item and it’s barcode, not both items with the same barcode.  
I have already found an item on your list that shows the barcode, but it is the correct barcode for that item – so where’s the other item showing the same barcode number?
0
 
LVL 28

Expert Comment

by:Pawan Kumar
ID: 41900074
Only for duplicates...


SELECT * FROM 
(
	SELECT * , COUNT(*) OVER (PARTITION BY Barcode) rnk 
	FROM Products
        WHERE LEN(Barcode) > 0
)k
WHERE k.rnk > 1

Open in new window

0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question